dsh-hive-runner 0.6.4 → 0.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +112 -0
- package/README.md +42 -13
- package/README.zh-CN.md +24 -9
- package/SPEC.md +95 -31
- package/cordis.patch.yml +14 -2
- package/docs/DESIGN.md +48 -3
- package/index.js +22 -0
- package/lib/commands.js +16 -9
- package/lib/config.js +27 -1
- package/lib/host-env.js +51 -22
- package/lib/http.js +18 -1
- package/lib/round.js +385 -121
- package/lib/runs.js +77 -4
- package/lib/supervisor.js +56 -0
- package/package.json +1 -1
package/docs/DESIGN.md
CHANGED
|
@@ -39,6 +39,8 @@ Response:
|
|
|
39
39
|
"repoRoleConcurrency": {},
|
|
40
40
|
"defaultRoleConcurrency": 5,
|
|
41
41
|
"capacityRetryMs": 2000,
|
|
42
|
+
"queenSessionId": "queen-session-id",
|
|
43
|
+
"waiting": null,
|
|
42
44
|
"lastTickAt": "2026-12-04T00:00:00.000Z",
|
|
43
45
|
"lastDiscoverError": null,
|
|
44
46
|
"repos": [
|
|
@@ -56,11 +58,26 @@ Response:
|
|
|
56
58
|
"consecutiveFailures": 0,
|
|
57
59
|
"lastCards": 0,
|
|
58
60
|
"lastResults": 0,
|
|
61
|
+
"waiting": null,
|
|
59
62
|
"backlog": { "queued": 3, "inFlight": 5, "completed": 12 },
|
|
60
63
|
"roles": {
|
|
61
64
|
"coder": { "limit": 5, "live": 5, "queued": 3 },
|
|
62
65
|
"tester": { "limit": 5, "live": 0, "queued": 0 }
|
|
63
66
|
},
|
|
67
|
+
"workers": [
|
|
68
|
+
{
|
|
69
|
+
"cardId": "readme-89ab",
|
|
70
|
+
"role": "coder",
|
|
71
|
+
"agent": "dsh",
|
|
72
|
+
"sessionId": "session-id",
|
|
73
|
+
"agentName": "agent-1",
|
|
74
|
+
"startedAt": "2026-12-04T00:00:03.000Z",
|
|
75
|
+
"lastOutputAt": "2026-12-04T00:02:00.000Z",
|
|
76
|
+
"heartbeat": "ok",
|
|
77
|
+
"claimDeadline": "2026-12-04T00:05:03.000Z",
|
|
78
|
+
"timeoutAt": "2026-12-04T00:15:03.000Z"
|
|
79
|
+
}
|
|
80
|
+
],
|
|
64
81
|
"limitsError": null
|
|
65
82
|
}
|
|
66
83
|
]
|
|
@@ -75,8 +92,22 @@ Response:
|
|
|
75
92
|
against `live`).
|
|
76
93
|
- `backlog` — aggregate `{ queued, inFlight, completed }` for the current or
|
|
77
94
|
last drain.
|
|
95
|
+
- `queenSessionId` — the queen conversation registered by the panel; every worker is
|
|
96
|
+
parented to exactly this Session. `waiting` is `"no-queen-session"` until the
|
|
97
|
+
registration arrives (startup-order hint, not an error); the supervisor never
|
|
98
|
+
falls back to another Session. Workers/run records carry `parentSource: "queen"`.
|
|
99
|
+
- `workers` — live workers in this repository: `cardId`, `role`, `agent`,
|
|
100
|
+
`sessionId`, `agentName`, `startedAt`, `lastOutputAt`, `heartbeat`
|
|
101
|
+
(`ok`/`warn`), `claimDeadline`, `timeoutAt`. The Session id is written into the
|
|
102
|
+
`.hive/runs/<cardId>.json` record as soon as the activation is accepted.
|
|
78
103
|
- `state` — `idle` | `running` | `backoff` | `paused`.
|
|
79
104
|
|
|
105
|
+
Every card has a hard `cardTimeoutMs` deadline (default 15 min); when it expires
|
|
106
|
+
the worker is aborted, the card is recorded failed and its announcements are released.
|
|
107
|
+
The claim TTL is observability only (`claimDeadline` in status) and never aborts a
|
|
108
|
+
running card. While a worker runs, the repository HEAD is polled as the output signal;
|
|
109
|
+
no movement for `heartbeatWarnMs` marks `heartbeat: "warn"` in status/run record.
|
|
110
|
+
|
|
80
111
|
### `POST ?op=pause` / `POST ?op=resume`
|
|
81
112
|
|
|
82
113
|
Body or query: `{ "repo": "/abs/repo" }`. Returns
|
|
@@ -98,6 +129,19 @@ Body:
|
|
|
98
129
|
- Malformed values → `400` `hive/invalid-role-limits`; non-POST → `405`
|
|
99
130
|
`hive/method-not-allowed`; unknown repo → `404`.
|
|
100
131
|
|
|
132
|
+
### `POST ?op=queen`
|
|
133
|
+
|
|
134
|
+
Body: `{ "sessionId": "<live queen Session id>" }`.
|
|
135
|
+
|
|
136
|
+
- Registers the queen conversation that owns every worker. The panel calls this
|
|
137
|
+
when its init/queen activation succeeds (or calls the `hiveSupervisor`
|
|
138
|
+
service's `setQueenSession()` from the host side). Registration is in-memory;
|
|
139
|
+
after a plugin restart the next panel access re-registers it.
|
|
140
|
+
- Until registration the supervisor does not dispatch: `status` reports
|
|
141
|
+
`waiting: "no-queen-session"` per repository and at the top level. This is a
|
|
142
|
+
startup-order hint, not an error; the supervisor never picks another Session.
|
|
143
|
+
- Returns `{ ok: true, op: "queen", queenSessionId, waiting: null, repos: ... }`.
|
|
144
|
+
- Missing `sessionId` -> `400` `hive/missing-session-id`; non-POST -> `405`.
|
|
101
145
|
## 3. Per-repository limits
|
|
102
146
|
|
|
103
147
|
Precedence for one role in one repository:
|
|
@@ -155,8 +199,8 @@ consuming the failure budget.
|
|
|
155
199
|
| `index.js` | Cordis plugin entry: config, limits store, supervisor service/timer, Fetch route, tools |
|
|
156
200
|
| `lib/supervisor.js` | Repository registry + state machine + per-repo queue/backlog/roles; exposes `setRoleLimits` |
|
|
157
201
|
| `lib/queue.js` | Live-only per-role worker pools, immediate slot refill, capacity requeue, per-role state callback |
|
|
158
|
-
| `lib/round.js` | One card end to end: claim → subagent activation → commit/acceptance verify → done/fail + last-run record |
|
|
159
|
-
| `lib/runs.js` | `<repo>/.hive/runs/<cardId>.json` last-run records: path validation, 200-line/32-KiB output truncation, optional worker `sessionId`/`agentName`, best-effort write |
|
|
202
|
+
| `lib/round.js` | One card end to end: claim → announcement/timeout guard (`hive guard` overlaps) + lesson read (`hive recall`) → subagent activation → commit/acceptance verify → done/fail + last-run record |
|
|
203
|
+
| `lib/runs.js` | `<repo>/.hive/runs/<cardId>.json` last-run records: path validation, 200-line/32-KiB output truncation, optional worker `sessionId`/`agentName`, advisory `overlaps`, `lessons` read summary, best-effort write |
|
|
160
204
|
| `lib/limits.js` | `<repo>/.hive/limits.json` read/write/hot-reload and precedence helpers |
|
|
161
205
|
| `lib/config.js` | Config defaults/validation, role map parsing, effective-limit helpers |
|
|
162
206
|
| `lib/commands.js` | Allowlisted `hive-tasks` / `git` / acceptance commands with shell quoting |
|
|
@@ -177,6 +221,7 @@ consuming the failure budget.
|
|
|
177
221
|
## 8. Tests
|
|
178
222
|
|
|
179
223
|
`node --test test/*.test.js`. Suites: supervisor state machine and limits
|
|
180
|
-
persistence/hot reload, live-only role queue, single-card round paths
|
|
224
|
+
persistence/hot reload, live-only role queue, single-card round paths (announcement
|
|
225
|
+
overlaps, lesson reads, timeouts), config,
|
|
181
226
|
HTTP protocol (including `op=limits` validation), discovery, command allowlist,
|
|
182
227
|
and an architecture guard (no child processes; README protocol hygiene).
|
package/index.js
CHANGED
|
@@ -43,12 +43,29 @@ export function apply(ctx, config = {}) {
|
|
|
43
43
|
repoRoleConcurrency: resolved.repoRoleConcurrency,
|
|
44
44
|
defaultRoleConcurrency: resolved.defaultRoleConcurrency,
|
|
45
45
|
});
|
|
46
|
+
const log = (level, ...args) => {
|
|
47
|
+
const write = console[level];
|
|
48
|
+
if (typeof write === 'function') write.call(console, '[hive-runner]', ...args);
|
|
49
|
+
else console.warn('[hive-runner]', ...args);
|
|
50
|
+
};
|
|
51
|
+
// The panel registers the queen conversation here (setQueenSession, also via
|
|
52
|
+
// POST ?op=queen). Every worker is parented to exactly that Session; before
|
|
53
|
+
// registration the supervisor does not dispatch at all.
|
|
54
|
+
const queenSession = { sessionId: null };
|
|
55
|
+
let parentProviderImpl;
|
|
46
56
|
const runner = createRoundRunner({
|
|
47
57
|
ctx,
|
|
48
58
|
config: resolved,
|
|
49
59
|
commands,
|
|
50
60
|
limitFor: (repo, role) => limits.limitFor(repo, role),
|
|
61
|
+
parentProvider: (explicit) => parentProviderImpl(explicit),
|
|
62
|
+
log,
|
|
51
63
|
});
|
|
64
|
+
parentProviderImpl = async (explicit) => {
|
|
65
|
+
const sessionId = queenSession.sessionId;
|
|
66
|
+
const agent = sessionId === null ? undefined : ctx.get('agents')?.get?.(sessionId);
|
|
67
|
+
return { agent, source: 'queen', sessionId };
|
|
68
|
+
};
|
|
52
69
|
|
|
53
70
|
const supervisor = new HiveSupervisor({
|
|
54
71
|
repos: resolved.repos,
|
|
@@ -70,6 +87,8 @@ export function apply(ctx, config = {}) {
|
|
|
70
87
|
listReady: runner.listReady,
|
|
71
88
|
runCard: runner.runCard,
|
|
72
89
|
preflight: runner.preflight,
|
|
90
|
+
activeRuns: (repo) => runner.activeRuns(repo),
|
|
91
|
+
queenSession,
|
|
73
92
|
readMode: (repo) => commands.mode(repo),
|
|
74
93
|
logger: console,
|
|
75
94
|
debug: resolved.debug,
|
|
@@ -119,6 +138,9 @@ export function apply(ctx, config = {}) {
|
|
|
119
138
|
const repo = repoOf(args);
|
|
120
139
|
const signal = exec?.signal;
|
|
121
140
|
const session = exec?.agent?.session;
|
|
141
|
+
if (supervisor.queenSession.sessionId === null) {
|
|
142
|
+
throw new Error('waiting: no-queen-session — open the Hive panel and initialise the repository first');
|
|
143
|
+
}
|
|
122
144
|
runner.preflight(repo);
|
|
123
145
|
const cards = await runner.listReady(repo, { signal, session });
|
|
124
146
|
const outcome = await drainQueue({
|
package/lib/commands.js
CHANGED
|
@@ -136,7 +136,7 @@ export function buildCommand(verb, args = {}, bins = {}) {
|
|
|
136
136
|
const agent = ' --agent ' + quote(needMatch(AGENT_RE, args.agent, 'guard.agent'));
|
|
137
137
|
const task = args.id === undefined || args.id === null ? '' : ' --task ' + quote(needMatch(ID_RE, args.id, 'guard.id'));
|
|
138
138
|
const ttl = args.ttl === undefined || args.ttl === null ? '' : ' --ttl ' + quote(needMatch(TTL_RE, args.ttl, 'guard.ttl'));
|
|
139
|
-
return hive + ' guard ' + keys + agent + task + ttl;
|
|
139
|
+
return hive + ' guard ' + keys + agent + task + ttl + ' --json';
|
|
140
140
|
}
|
|
141
141
|
case 'release':
|
|
142
142
|
return hive + ' release ' + scopeArgs(args.paths, 'release.paths')
|
|
@@ -241,14 +241,19 @@ export function createCommands({ ctx, config }) {
|
|
|
241
241
|
/** Execute one allowlisted verb. @returns {Promise<object>} the raw shell result */
|
|
242
242
|
async function execute(verb, args, options = {}) {
|
|
243
243
|
const command = buildCommand(verb, args, bins);
|
|
244
|
+
// Every Hive/git command carries the claim identity explicitly. Per-card
|
|
245
|
+
// Hive calls also carry a per-card `HIVE_INSTANCE` (the card id), so
|
|
246
|
+
// concurrent workers of one runner are distinct announcers on the consensus
|
|
247
|
+
// board instead of looking like the same instance.
|
|
248
|
+
const env = { HIVE_AGENT: options.agent ?? config.agent };
|
|
249
|
+
const instance = typeof options.instance === 'string' ? options.instance.trim() : '';
|
|
250
|
+
if (instance !== '') env.HIVE_INSTANCE = instance;
|
|
244
251
|
const shellOptions = {
|
|
245
252
|
workdir: options.repo,
|
|
246
253
|
signal: options.signal,
|
|
247
254
|
session: options.session,
|
|
248
255
|
timeoutMs: options.timeoutMs ?? config.cliTimeoutMs,
|
|
249
|
-
|
|
250
|
-
// pre-commit hook must never fall back to "HIVE_AGENT is not set".
|
|
251
|
-
env: { HIVE_AGENT: options.agent ?? config.agent, ...(options.env ?? {}) },
|
|
256
|
+
env: { ...env, ...(options.env ?? {}) },
|
|
252
257
|
};
|
|
253
258
|
let result = await run(command, shellOptions);
|
|
254
259
|
if (!missingBinary(result)) return result;
|
|
@@ -319,9 +324,11 @@ export function createCommands({ ctx, config }) {
|
|
|
319
324
|
return value ?? [];
|
|
320
325
|
},
|
|
321
326
|
/**
|
|
322
|
-
* `hive guard <scope...> --task <id> --ttl <ttl
|
|
323
|
-
*
|
|
324
|
-
*
|
|
327
|
+
* `hive guard <scope...> --task <id> --ttl <ttl> --json`: announce the
|
|
328
|
+
* card's scope and publish the board as working. hive-consensus 1.5.0 never
|
|
329
|
+
* blocks on an announcement, so the raw result is returned as-is; the
|
|
330
|
+
* `--json` document carries the advisory `overlaps` array the runner
|
|
331
|
+
* records, and a non-zero exit is only an unexpected CLI problem.
|
|
325
332
|
*/
|
|
326
333
|
async guard(repo, paths, options = {}) {
|
|
327
334
|
return execute('guard', {
|
|
@@ -331,11 +338,11 @@ export function createCommands({ ctx, config }) {
|
|
|
331
338
|
ttl: options.ttl ?? config.ttl,
|
|
332
339
|
}, { ...options, repo });
|
|
333
340
|
},
|
|
334
|
-
/** `hive release <scope...> --agent A`:
|
|
341
|
+
/** `hive release <scope...> --agent A`: clear our announcements (best effort). */
|
|
335
342
|
async release(repo, paths, options = {}) {
|
|
336
343
|
return execute('release', { paths, agent: options.agent ?? config.agent }, { ...options, repo });
|
|
337
344
|
},
|
|
338
|
-
/** `hive renew <scope...> --agent A --ttl T`:
|
|
345
|
+
/** `hive renew <scope...> --agent A --ttl T`: refresh announcements we already published. */
|
|
339
346
|
async renew(repo, paths, options = {}) {
|
|
340
347
|
return execute('renew', {
|
|
341
348
|
paths,
|
package/lib/config.js
CHANGED
|
@@ -18,6 +18,12 @@ export const DEFAULT_MAX_CONCURRENT_REPOS = 2;
|
|
|
18
18
|
export const DEFAULT_MAX_CONSECUTIVE_FAILURES = 3;
|
|
19
19
|
/** Default capacity retry delay when DSH's subagent capacity is exhausted. */
|
|
20
20
|
export const DEFAULT_CAPACITY_RETRY_MS = 2_000;
|
|
21
|
+
/** Hard per-card wall-clock limit: abort the worker and fail the card at the deadline. */
|
|
22
|
+
export const DEFAULT_CARD_TIMEOUT_MS = 15 * 60_000;
|
|
23
|
+
/** Warn when a running worker produced no new commit/output for this long. */
|
|
24
|
+
export const DEFAULT_HEARTBEAT_WARN_MS = 5 * 60_000;
|
|
25
|
+
/** How often heartbeat/liveness is checked while a worker runs. */
|
|
26
|
+
export const DEFAULT_HEARTBEAT_INTERVAL_MS = 30_000;
|
|
21
27
|
/** Default role -> concurrent live-worker cap, per repository. */
|
|
22
28
|
export const DEFAULT_ROLE_CONCURRENCY = Object.freeze({ coder: 5, tester: 5 });
|
|
23
29
|
/** Fallback cap for roles not listed in `roleConcurrency` (also finite). */
|
|
@@ -37,7 +43,10 @@ const DEFAULTS = Object.freeze({
|
|
|
37
43
|
maxConsecutiveFailures: DEFAULT_MAX_CONSECUTIVE_FAILURES,
|
|
38
44
|
paused: [],
|
|
39
45
|
agent: 'dsh',
|
|
40
|
-
|
|
46
|
+
// Claim TTL: bounds how long a claim survives while a card waits for a worker
|
|
47
|
+
// slot. It never aborts a running card (the hard cardTimeoutMs does that);
|
|
48
|
+
// raise it for very large queues.
|
|
49
|
+
ttl: '20m',
|
|
41
50
|
// Finite, user-set default: five live workers per role per repository
|
|
42
51
|
// (repo-level .hive/limits.json overrides this at runtime). Repository
|
|
43
52
|
// overrides use absolute-path keys with a role-cap object, e.g.
|
|
@@ -46,6 +55,9 @@ const DEFAULTS = Object.freeze({
|
|
|
46
55
|
// Fallback cap for roles not listed in roleConcurrency (also finite).
|
|
47
56
|
defaultRoleConcurrency: DEFAULT_DEFAULT_ROLE_CONCURRENCY,
|
|
48
57
|
capacityRetryMs: DEFAULT_CAPACITY_RETRY_MS,
|
|
58
|
+
cardTimeoutMs: DEFAULT_CARD_TIMEOUT_MS,
|
|
59
|
+
heartbeatWarnMs: DEFAULT_HEARTBEAT_WARN_MS,
|
|
60
|
+
heartbeatIntervalMs: DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
49
61
|
provider: 'spawn',
|
|
50
62
|
parentSession: null,
|
|
51
63
|
cwd: null,
|
|
@@ -202,6 +214,17 @@ function bin(value, name, env, fallback) {
|
|
|
202
214
|
return nonEmptyString(value, name);
|
|
203
215
|
}
|
|
204
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Parse a claim TTL string ("30s", "5m", "2h", "1d") to milliseconds.
|
|
219
|
+
* @param {string} ttl
|
|
220
|
+
* @returns {number}
|
|
221
|
+
*/
|
|
222
|
+
export function parseTtlMs(ttl) {
|
|
223
|
+
const match = /^(\d{1,4})([smhd])$/.exec(String(ttl ?? ''));
|
|
224
|
+
if (match === null) return 5 * 60_000;
|
|
225
|
+
return Number(match[1]) * ({ s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000 }[match[2]] ?? 60_000);
|
|
226
|
+
}
|
|
227
|
+
|
|
205
228
|
/**
|
|
206
229
|
* Normalize one repository key. Configuration requires absolute paths; HTTP and
|
|
207
230
|
* tool callers may send any string, resolved against the host working directory.
|
|
@@ -241,6 +264,9 @@ export function resolveConfig(config = {}) {
|
|
|
241
264
|
fail('defaultRoleConcurrency must be an integer in 0..999 (0 pauses unlisted roles)');
|
|
242
265
|
}
|
|
243
266
|
cfg.capacityRetryMs = positiveNumber(cfg.capacityRetryMs, 'capacityRetryMs');
|
|
267
|
+
cfg.cardTimeoutMs = positiveNumber(cfg.cardTimeoutMs, 'cardTimeoutMs');
|
|
268
|
+
cfg.heartbeatWarnMs = positiveNumber(cfg.heartbeatWarnMs, 'heartbeatWarnMs');
|
|
269
|
+
cfg.heartbeatIntervalMs = positiveNumber(cfg.heartbeatIntervalMs, 'heartbeatIntervalMs');
|
|
244
270
|
cfg.cliTimeoutMs = positiveNumber(cfg.cliTimeoutMs, 'cliTimeoutMs');
|
|
245
271
|
cfg.headTimeoutMs = positiveNumber(cfg.headTimeoutMs, 'headTimeoutMs');
|
|
246
272
|
cfg.acceptanceTimeoutMs = positiveNumber(cfg.acceptanceTimeoutMs, 'acceptanceTimeoutMs');
|
package/lib/host-env.js
CHANGED
|
@@ -1,45 +1,74 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Scoped `HIVE_AGENT` injection for worker subagents.
|
|
2
|
+
* Scoped `HIVE_AGENT` / `HIVE_INSTANCE` injection for worker subagents.
|
|
3
3
|
*
|
|
4
4
|
* DSH's subagent seam runs workers in the host process and the model-facing
|
|
5
5
|
* bash tool inherits ambient environment variables (grep/subprocess scrub only
|
|
6
|
-
* credential-shaped and DSH_* names). The plugin therefore injects
|
|
7
|
-
*
|
|
8
|
-
* `
|
|
9
|
-
*
|
|
6
|
+
* credential-shaped and DSH_* names). The plugin therefore injects the identity
|
|
7
|
+
* explicitly while a worker is alive, so the worker's `hive guard`,
|
|
8
|
+
* `hive release`, `hive recall` and `git commit` commands agree with the
|
|
9
|
+
* runner's own consensus calls.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* `HIVE_AGENT` is the shared runner identity; `HIVE_INSTANCE` distinguishes
|
|
12
|
+
* the concurrent workers of one runner. hive-consensus falls back to the agent
|
|
13
|
+
* name when `HIVE_INSTANCE` is unset, which would make two workers of the same
|
|
14
|
+
* runner look like the same announcer and hide every overlap — so the runner
|
|
15
|
+
* passes the card id as the per-card instance.
|
|
16
|
+
*
|
|
17
|
+
* The values are reference-counted; the previous environment is restored when
|
|
18
|
+
* the last worker finishes. Several workers can be alive at once in one drain,
|
|
19
|
+
* so the most recently acquired identity is the one visible to the model-facing
|
|
20
|
+
* bash tool (the runner's own Hive commands always pass the identity explicitly).
|
|
14
21
|
*/
|
|
15
22
|
|
|
16
|
-
|
|
23
|
+
const holders = [];
|
|
17
24
|
let saved;
|
|
18
25
|
|
|
26
|
+
function remember() {
|
|
27
|
+
return { agent: process.env.HIVE_AGENT, instance: process.env.HIVE_INSTANCE };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function apply(holder) {
|
|
31
|
+
process.env.HIVE_AGENT = holder.agent;
|
|
32
|
+
process.env.HIVE_INSTANCE = holder.instance;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function restore(values) {
|
|
36
|
+
if (values === undefined) return;
|
|
37
|
+
if (values.agent === undefined) delete process.env.HIVE_AGENT;
|
|
38
|
+
else process.env.HIVE_AGENT = values.agent;
|
|
39
|
+
if (values.instance === undefined) delete process.env.HIVE_INSTANCE;
|
|
40
|
+
else process.env.HIVE_INSTANCE = values.instance;
|
|
41
|
+
}
|
|
42
|
+
|
|
19
43
|
/**
|
|
20
|
-
* Set `
|
|
44
|
+
* Set `HIVE_AGENT` and `HIVE_INSTANCE` for the duration of one worker.
|
|
21
45
|
* @param {string} agent claim identity
|
|
46
|
+
* @param {string} [instance] per-card instance (the card id); defaults to the agent
|
|
22
47
|
* @returns {() => void} idempotent release
|
|
23
48
|
*/
|
|
24
|
-
export function acquireHiveAgentEnv(agent) {
|
|
49
|
+
export function acquireHiveAgentEnv(agent, instance) {
|
|
25
50
|
if (typeof agent !== 'string' || agent.trim() === '') return () => {};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
51
|
+
const agentName = agent.trim();
|
|
52
|
+
const instanceName = typeof instance === 'string' && instance.trim() !== '' ? instance.trim() : agentName;
|
|
53
|
+
if (holders.length === 0) saved = remember();
|
|
54
|
+
const holder = { agent: agentName, instance: instanceName, released: false };
|
|
55
|
+
holders.push(holder);
|
|
56
|
+
apply(holder);
|
|
30
57
|
return () => {
|
|
31
|
-
if (released) return;
|
|
32
|
-
released = true;
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
58
|
+
if (holder.released) return;
|
|
59
|
+
holder.released = true;
|
|
60
|
+
const index = holders.indexOf(holder);
|
|
61
|
+
if (index !== -1) holders.splice(index, 1);
|
|
62
|
+
if (holders.length === 0) {
|
|
63
|
+
restore(saved);
|
|
37
64
|
saved = undefined;
|
|
65
|
+
} else {
|
|
66
|
+
apply(holders[holders.length - 1]);
|
|
38
67
|
}
|
|
39
68
|
};
|
|
40
69
|
}
|
|
41
70
|
|
|
42
71
|
/** Current number of active worker env holders (diagnostics/tests). */
|
|
43
72
|
export function activeHiveAgentHolders() {
|
|
44
|
-
return
|
|
73
|
+
return holders.length;
|
|
45
74
|
}
|
package/lib/http.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* POST /api/hive-runner?op=pause { repo }
|
|
10
10
|
* POST /api/hive-runner?op=resume { repo }
|
|
11
11
|
* POST /api/hive-runner?op=limits { repo, roleLimits: { role: number } }
|
|
12
|
+
* POST /api/hive-runner?op=queen { sessionId }
|
|
12
13
|
*/
|
|
13
14
|
import { normalizeRepoKey } from './config.js';
|
|
14
15
|
import { UnknownRepoError } from './supervisor.js';
|
|
@@ -74,6 +75,22 @@ export async function handleHiveRunnerRequest(request, supervisor) {
|
|
|
74
75
|
return failure(op, 'hive/internal', error instanceof Error ? error.message : String(error), 500);
|
|
75
76
|
}
|
|
76
77
|
}
|
|
78
|
+
if (op === 'queen') {
|
|
79
|
+
if (request.method !== 'POST') {
|
|
80
|
+
return failure(op, 'hive/method-not-allowed', 'op=queen requires POST', 405);
|
|
81
|
+
}
|
|
82
|
+
const sessionId = typeof payload.sessionId === 'string'
|
|
83
|
+
? payload.sessionId
|
|
84
|
+
: url.searchParams.get('sessionId');
|
|
85
|
+
if (typeof sessionId !== 'string' || sessionId.trim() === '') {
|
|
86
|
+
return failure(op, 'hive/missing-session-id', 'sessionId is required (JSON body field)', 400);
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
return json({ ok: true, op: 'queen', ...supervisor.setQueenSession(sessionId) });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return failure(op, 'hive/invalid-session-id', error instanceof Error ? error.message : String(error), 400);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
77
94
|
if (op === 'limits') {
|
|
78
95
|
if (request.method !== 'POST') {
|
|
79
96
|
return failure(op, 'hive/method-not-allowed', 'op=limits requires POST', 405);
|
|
@@ -103,7 +120,7 @@ export async function handleHiveRunnerRequest(request, supervisor) {
|
|
|
103
120
|
return failure(op, 'hive/internal', error instanceof Error ? error.message : String(error), 500);
|
|
104
121
|
}
|
|
105
122
|
}
|
|
106
|
-
return failure(op, 'hive/unknown-op', 'unknown op ' + JSON.stringify(op) + '; use status, pause, resume or limits', 400);
|
|
123
|
+
return failure(op, 'hive/unknown-op', 'unknown op ' + JSON.stringify(op) + '; use status, queen, pause, resume or limits', 400);
|
|
107
124
|
}
|
|
108
125
|
|
|
109
126
|
/**
|