dsh-hive-runner 0.6.1 → 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.
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Scoped `HIVE_AGENT` / `HIVE_INSTANCE` injection for worker subagents.
3
+ *
4
+ * DSH's subagent seam runs workers in the host process and the model-facing
5
+ * bash tool inherits ambient environment variables (grep/subprocess scrub only
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
+ *
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).
21
+ */
22
+
23
+ const holders = [];
24
+ let saved;
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
+
43
+ /**
44
+ * Set `HIVE_AGENT` and `HIVE_INSTANCE` for the duration of one worker.
45
+ * @param {string} agent claim identity
46
+ * @param {string} [instance] per-card instance (the card id); defaults to the agent
47
+ * @returns {() => void} idempotent release
48
+ */
49
+ export function acquireHiveAgentEnv(agent, instance) {
50
+ if (typeof agent !== 'string' || agent.trim() === '') return () => {};
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);
57
+ return () => {
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);
64
+ saved = undefined;
65
+ } else {
66
+ apply(holders[holders.length - 1]);
67
+ }
68
+ };
69
+ }
70
+
71
+ /** Current number of active worker env holders (diagnostics/tests). */
72
+ export function activeHiveAgentHolders() {
73
+ return holders.length;
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
  /**