hivecode-mcp 0.12.2 → 0.13.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.
Files changed (62) hide show
  1. package/HIVE_RULES.md +11 -40
  2. package/LICENSE +21 -21
  3. package/README.md +421 -167
  4. package/core.js +91 -91
  5. package/docs/ALWAYS_ON.md +310 -0
  6. package/docs/GITHUB_WORKFLOW.md +334 -0
  7. package/docs/HIVE_RULES.md +41 -41
  8. package/docs/MCP.md +157 -118
  9. package/docs/QUICKSTART.md +266 -0
  10. package/docs/SELF_HOSTING.md +256 -256
  11. package/docs/SLACK_SETUP.md +450 -0
  12. package/docs/TROUBLESHOOTING.md +87 -0
  13. package/hive-activity.js +336 -310
  14. package/hive-await.js +207 -0
  15. package/hive-branch.js +686 -0
  16. package/hive-busy.js +88 -88
  17. package/hive-coord.js +110 -110
  18. package/hive-gate.js +135 -135
  19. package/hive-github.js +1904 -0
  20. package/hive-hook-install.js +179 -103
  21. package/hive-instructions.js +74 -66
  22. package/hive-listen.js +81 -0
  23. package/hive-mailbox.js +520 -386
  24. package/hive-mcp-http.js +580 -0
  25. package/hive-mcp.js +579 -137
  26. package/hive-mux-proto.js +58 -58
  27. package/hive-mux.js +350 -350
  28. package/hive-notify.js +233 -0
  29. package/hive-obligations.js +247 -247
  30. package/hive-pr-staleness.js +189 -189
  31. package/hive-pr.js +350 -0
  32. package/hive-privacy.js +327 -288
  33. package/hive-receipts.js +233 -233
  34. package/hive-relay-health.js +230 -225
  35. package/hive-room-key.js +209 -0
  36. package/hive-secrets.js +89 -0
  37. package/hive-session.js +114 -0
  38. package/hive-setup.js +558 -445
  39. package/hive-slack-agent.js +396 -0
  40. package/hive-slack.js +1809 -0
  41. package/hive-stdio.js +172 -168
  42. package/hive-stop-hook.js +293 -241
  43. package/hive-throttle.js +118 -118
  44. package/hive-time.js +71 -0
  45. package/hive-update.js +58 -58
  46. package/hive-version.js +110 -59
  47. package/hive-webhook.js +54 -0
  48. package/icr-merge.js +58 -58
  49. package/icr.js +851 -745
  50. package/lang-json.js +211 -211
  51. package/lang-ruby.js +230 -230
  52. package/lang-toml.js +136 -136
  53. package/lang-yaml.js +183 -183
  54. package/limits.js +413 -394
  55. package/package.json +189 -164
  56. package/reconcile.cjs +614 -477
  57. package/slack-app-manifest-agent.json +61 -0
  58. package/slack-app-manifest-inbound.json +36 -0
  59. package/slack-app-manifest.json +27 -0
  60. package/substrate.js +252 -252
  61. package/sync.js +1956 -177
  62. package/grm-link.cjs +0 -141
@@ -0,0 +1,209 @@
1
+ // PERSONAL ROOMS (8 Oct 2026): a room that only the people its host invites can enter.
2
+ //
3
+ // The relay already enforces this for any room whose id is "hs_<fp>_<rand>". fp is a fingerprint of
4
+ // the host's public key, every token for the room carries that key, and only the matching private
5
+ // key can sign one (server.js authorize). So a stranger holding the room's name gets nothing, even
6
+ // on the open public relay. What was missing was a way for an AGENT to make such a room: until now
7
+ // only the VS Code extension could, holding the key in the editor's secret storage.
8
+ //
9
+ // THE KEY NEVER LIVES IN A ROOM FOLDER. It sits in ~/.hivecode/keys/<room>.json, outside every
10
+ // folder the sync engine shares, so no client -- old, new or misconfigured -- can publish it.
11
+ // Whoever holds that file owns the room: lose it and nobody can invite again; copy it and the copy
12
+ // can invite too.
13
+ //
14
+ // INVITES ARE A COMMAND, NOT A TOOL. Every tool definition is paid on every request by every agent
15
+ // (tests/token-budget-test.mjs holds that to 3,500 tokens), so inviting -- something a host does a
16
+ // few times a week -- is `hivecode-mcp invite <name>`, run by the person or by an agent that has a
17
+ // shell. One link per person, carrying a token minted for them alone: an invite IS that person,
18
+ // which is why it is never pasted into the room chat (hive_say refuses tokens).
19
+ import fs from 'fs'
20
+ import os from 'os'
21
+ import path from 'path'
22
+ import crypto from 'crypto'
23
+ import { sign, decodeUnsafe, keyFingerprint, roomFingerprint, isSecuredRoom, makeSecuredRoomId } from './token.js'
24
+ import { validateMemberName } from './hive-mailbox.js'
25
+
26
+ export const keysDir = () => process.env.HIVE_KEYS_DIR || path.join(os.homedir(), '.hivecode', 'keys')
27
+ const keyFile = (room) => path.join(keysDir(), `${room}.json`)
28
+ const DAY = 86400
29
+ const nowSec = () => Math.floor(Date.now() / 1000)
30
+
31
+ /** A new personal room, in memory only: nothing is written until the join is allowed to go ahead. */
32
+ export function newPersonalRoom() {
33
+ const kp = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 })
34
+ const publicKey = kp.publicKey.export({ type: 'spki', format: 'pem' })
35
+ const privateKey = kp.privateKey.export({ type: 'pkcs8', format: 'pem' })
36
+ return { room: makeSecuredRoomId(publicKey, crypto.randomBytes(6).toString('hex')), publicKey, privateKey }
37
+ }
38
+
39
+ export function saveRoomKey(k, { relay, host }) {
40
+ fs.mkdirSync(keysDir(), { recursive: true, mode: 0o700 })
41
+ const rec = { room: k.room, relay, host, publicKey: k.publicKey, privateKey: k.privateKey, created: Date.now(), invites: [] }
42
+ // 'wx': a fresh room never overwrites a key that exists, which would orphan the room it opened.
43
+ fs.writeFileSync(keyFile(k.room), JSON.stringify(rec, null, 2), { mode: 0o600, flag: 'wx' })
44
+ return rec
45
+ }
46
+
47
+ /** The key record for a personal room this machine hosts, or null. A file whose key does not match
48
+ * the room's fingerprint is treated as absent: it would only mint tokens the relay refuses. */
49
+ export function loadRoomKey(room) {
50
+ if (!isSecuredRoom(room)) return null
51
+ try {
52
+ const rec = JSON.parse(fs.readFileSync(keyFile(room), 'utf8'))
53
+ return rec && rec.privateKey && keyFingerprint(rec.publicKey) === roomFingerprint(room) ? rec : null
54
+ } catch { return null }
55
+ }
56
+ const writeRec = (rec) => fs.writeFileSync(keyFile(rec.room), JSON.stringify(rec, null, 2), { mode: 0o600 })
57
+
58
+ /** The same claims the extension mints (extension.js mintToken), so either can host and the relay
59
+ * cannot tell them apart. paths = what they may SEE; writePaths = the part of it they may EDIT. */
60
+ export function mintRoomToken(rec, { name, kind = 'ai', role = 'writer', paths, writePaths, owner, ttlSec = 7 * DAY }) {
61
+ const now = nowSec()
62
+ const jti = 'jti-' + crypto.randomBytes(9).toString('base64url')
63
+ const payload = {
64
+ iss: 'hivecode', sub: name, name, kind, ...(owner ? { owner } : {}),
65
+ pk: rec.publicKey, // self-certifying: the relay binds this key to the room id
66
+ scopes: [{ room: rec.room, role, ...(paths && paths.length ? { paths } : {}), ...(writePaths && writePaths.length ? { writePaths } : {}) }],
67
+ iat: now, exp: now + ttlSec, jti,
68
+ }
69
+ return { token: sign(payload, { privateKey: rec.privateKey }), jti, exp: payload.exp }
70
+ }
71
+
72
+ /** The host's own token, minted afresh when it is missing or within an hour of running out: the host
73
+ * holds the key, so its own access must never lapse. Only for the name the room was hosted under --
74
+ * another participant on this machine, joining with its own invite, keeps its own narrower grant. */
75
+ export function hostToken(room, name, current, { kind = 'ai', owner = '' } = {}) {
76
+ const rec = loadRoomKey(room)
77
+ if (!rec || rec.host !== name) return current
78
+ const p = current ? decodeUnsafe(current) : null
79
+ const fresh = !!(p && p.sub === name && p.exp > nowSec() + 3600 && p.pk && keyFingerprint(p.pk) === roomFingerprint(room))
80
+ return fresh ? current : mintRoomToken(rec, { name, kind, role: 'maintainer', owner, ttlSec: 30 * DAY }).token
81
+ }
82
+
83
+ // A path a token may name: relative, no "..", no drive or root. Globs are fine ("src/**", "!**/*.env").
84
+ function badPath(g) {
85
+ const s = String(g).replace(/^!/, '')
86
+ return !s || s.length > 200 || /^([A-Za-z]:|[\\/])/.test(s) || /(^|[\\/])\.\.([\\/]|$)/.test(s) || /[\x00-\x1f]/.test(s)
87
+ }
88
+ const list = (v) => (v == null || v === true ? [] : String(v).split(',').map((s) => s.trim()).filter(Boolean))
89
+ const when = (sec) => new Date(sec * 1000).toISOString().slice(0, 10)
90
+
91
+ function whatTheyGet(i) {
92
+ const see = i.paths && i.paths.length ? i.paths.join(', ') : 'every file in the room'
93
+ const edit = i.role === 'reader' ? 'read only' : i.writePaths && i.writePaths.length ? `may edit ${i.writePaths.join(', ')}` : 'may edit what they see'
94
+ return `${i.kind}, ${i.role}: sees ${see}; ${edit}`
95
+ }
96
+
97
+ /** Make one person's invite. Returns { ok, text, link?, jti? }. */
98
+ export function makeInvite({ room, relay, name, kind = 'ai', reader = false, paths = [], writePaths = [], days = 7, owner = '' }) {
99
+ const rec = loadRoomKey(room)
100
+ if (!rec) {
101
+ return { ok: false, text: isSecuredRoom(room)
102
+ ? `Only the machine that hosted this personal room can invite people to it, and this one does not hold its key (${keysDir()}). Ask the host.`
103
+ : `Room ${room} is open: anyone with its link can join, so there is nothing to invite into. For a room only invited people can enter, host one with hive_join and private: true.` }
104
+ }
105
+ const nc = validateMemberName(name)
106
+ if (!nc.ok) return { ok: false, text: `${nc.error}\nNothing was minted.` }
107
+ if (name === rec.host) return { ok: false, text: `"${name}" is the host of this room. Pick the invitee's own name.\nNothing was minted.` }
108
+ const bad = [...paths, ...writePaths].filter(badPath)
109
+ if (bad.length) return { ok: false, text: `These paths cannot go in an invite (they must be relative, with no ".."): ${bad.join(', ')}\nNothing was minted.` }
110
+ const d = Number(days)
111
+ if (!Number.isFinite(d) || d <= 0 || d > 30) return { ok: false, text: `--days must be between 1 and 30 (got ${days}). A short invite is one you never have to remember to take back.\nNothing was minted.` }
112
+ const k = kind === 'human' ? 'human' : 'ai'
113
+ const role = reader ? 'reader' : 'writer'
114
+ const { token, jti, exp } = mintRoomToken(rec, { name, kind: k, role, paths, writePaths, owner, ttlSec: Math.round(d * DAY) })
115
+ const inv = { jti, name, kind: k, role, paths: paths.length ? paths : null, writePaths: writePaths.length ? writePaths : null, exp, at: Date.now() }
116
+ rec.invites = [...(rec.invites || []), inv]
117
+ writeRec(rec)
118
+ const link = `${relay || rec.relay}|${room}|${token}`
119
+ const text = `INVITE for ${name} (${whatTheyGet(inv)}), until ${when(exp)}.
120
+ Send it to ${name} directly, never in the room chat: whoever holds this link joins as ${name}.
121
+
122
+ Join my Hivecode room. In a terminal, run this once (needs Node.js 18+), then do what it prints:
123
+ npx -y hivecode-mcp@latest setup "${link}"
124
+
125
+ Take it back any time: npx -y hivecode-mcp@latest revoke ${name}`
126
+ return { ok: true, text, link, jti }
127
+ }
128
+
129
+ const httpBase = (relay) => String(relay).replace(/^ws(s?):\/\//i, 'http$1://').replace(/\/+$/, '')
130
+
131
+ /** Take back every live invite for a name. The relay refuses those tokens from then on and closes any
132
+ * connection using them. Returns { ok, text }. */
133
+ export async function revokeInvite({ room, relay, name }) {
134
+ const rec = loadRoomKey(room)
135
+ if (!rec) return { ok: false, text: `This machine does not host room ${room}, so it cannot take invites back. Only the host can.` }
136
+ const live = (rec.invites || []).filter((i) => i.name === name && !i.revokedAt)
137
+ if (!live.length) {
138
+ const names = [...new Set((rec.invites || []).filter((i) => !i.revokedAt).map((i) => i.name))]
139
+ return { ok: false, text: `No live invite for "${name}" in this room.${names.length ? ` Invited: ${names.join(', ')}.` : ' Nobody has been invited yet.'}` }
140
+ }
141
+ const owner = mintRoomToken(rec, { name: rec.host, role: 'maintainer', ttlSec: 600 }).token
142
+ let done = 0, dropped = 0
143
+ const failures = []
144
+ for (const i of live) {
145
+ try {
146
+ const r = await fetch(`${httpBase(relay || rec.relay)}/__hive/revoke`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ room, token: owner, jti: i.jti }) })
147
+ const body = await r.json().catch(() => ({}))
148
+ if (r.ok && body.ok) { i.revokedAt = Date.now(); done++; dropped += Number(body.dropped) || 0 }
149
+ else failures.push(`HTTP ${r.status}${body.error ? ` ${body.error}` : ''}`)
150
+ } catch (e) { failures.push(e.message) }
151
+ }
152
+ writeRec(rec)
153
+ if (!done) return { ok: false, text: `Could not reach the relay to revoke ${name}'s invite (${failures.join('; ')}). Nothing changed; try again.` }
154
+ return { ok: true, text: `Revoked ${done} invite(s) for ${name}. The relay refuses them from now on${dropped ? `, and closed ${dropped} connection(s) using them` : ''}.${failures.length ? `\n${failures.length} more could not be revoked (${failures.join('; ')}); run this again.` : ''}` }
155
+ }
156
+
157
+ // ---- the command line: hivecode-mcp invite <name> ... | hivecode-mcp revoke <name> ----
158
+
159
+ function parseArgs(argv) {
160
+ const o = { _: [] }
161
+ for (let i = 0; i < argv.length; i++) {
162
+ const a = argv[i]
163
+ if (!a.startsWith('--')) { o._.push(a); continue }
164
+ const key = a.slice(2)
165
+ if (['reader', 'human'].includes(key)) o[key] = true
166
+ else o[key] = argv[++i]
167
+ }
168
+ return o
169
+ }
170
+
171
+ // Which room: --dir's .hive.json, else this folder's, else the only room this machine hosts.
172
+ function findRoom(o) {
173
+ const dir = path.resolve(o.dir || '.')
174
+ try {
175
+ const c = JSON.parse(fs.readFileSync(path.join(dir, '.hive.json'), 'utf8'))
176
+ if (c && c.room) return { room: c.room, relay: c.relay || '' }
177
+ } catch { /* not a room folder */ }
178
+ let mine = []
179
+ try { mine = fs.readdirSync(keysDir()).filter((f) => f.endsWith('.json')).map((f) => loadRoomKey(f.slice(0, -5))).filter(Boolean) } catch { /* no keys yet */ }
180
+ if (mine.length === 1) return { room: mine[0].room, relay: mine[0].relay }
181
+ return { error: mine.length
182
+ ? `${dir} is not a room folder, and this machine hosts ${mine.length} personal rooms. Say which with --dir <the room's folder>:\n${mine.map((r) => ` ${r.room} (host ${r.host}, created ${new Date(r.created).toISOString().slice(0, 10)})`).join('\n')}`
183
+ : `${dir} is not a room folder, and this machine hosts no personal room. Host one first: hive_join with private: true.` }
184
+ }
185
+
186
+ const USAGE = `Usage:
187
+ hivecode-mcp invite <name> [--reader] [--human] [--paths a,b] [--edit a,b] [--days 7] [--owner <their human>] [--dir <room folder>]
188
+ hivecode-mcp revoke <name> [--dir <room folder>]
189
+
190
+ --reader they can read but not edit --paths only these folders/globs are visible to them
191
+ --human the invitee is a person, not AI --edit of what they see, only these may be edited
192
+ --days how long the invite lasts (default 7, at most 30)`
193
+
194
+ export async function main(argv) {
195
+ const cmd = argv[0]
196
+ const o = parseArgs(argv.slice(1))
197
+ const name = o._[0]
198
+ if (!name) { console.log(USAGE); return 2 }
199
+ const where = findRoom(o)
200
+ if (where.error) { console.log(where.error); return 1 }
201
+ if (cmd === 'revoke') {
202
+ const r = await revokeInvite({ room: where.room, relay: where.relay, name })
203
+ console.log(r.text)
204
+ return r.ok ? 0 : 1
205
+ }
206
+ const r = makeInvite({ room: where.room, relay: where.relay, name, kind: o.human ? 'human' : 'ai', reader: !!o.reader, paths: list(o.paths), writePaths: list(o.edit), days: o.days == null ? 7 : o.days, owner: o.owner || '' })
207
+ console.log(r.text)
208
+ return r.ok ? 0 : 1
209
+ }
@@ -0,0 +1,89 @@
1
+ // LIVE CREDENTIALS ARE NOT SENT TO THE ROOM (3 Oct 2026).
2
+ //
3
+ // A file in a room goes to the relay and to every participant's disk. ALWAYS_IGNORE keeps the
4
+ // files that are secrets by NAME at home (.env, *.pem, id_rsa, ...), but a secret inside an
5
+ // ordinary file went out with it: an agent that pastes an API key into config.js to try
6
+ // something shares that key with everyone in the room, and with whoever runs the relay.
7
+ //
8
+ // So a file whose content holds a credential the room does not already have is held on this
9
+ // machine (see sync.js, withheld) until the credential is moved out. Only formats a provider
10
+ // stamps on its keys are matched -- a fixed prefix plus enough random characters -- and private
11
+ // key blocks. Nothing here guesses from entropy alone: a false match holds back real work, and
12
+ // the people who hit it would be right to switch this off.
13
+ //
14
+ // The matched value never leaves this module's callers in any form: not in a log line, not in
15
+ // the room, not in status. Only its kind and its line number do.
16
+
17
+ const PATTERNS = [
18
+ { kind: 'private key', re: /-----BEGIN (?:[A-Z0-9]+ ){0,3}PRIVATE KEY(?: BLOCK)?-----/g },
19
+ { kind: 'AWS access key', re: /\b(?:AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}\b/g },
20
+ { kind: 'GitHub token', re: /\b(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{60,100})\b/g },
21
+ { kind: 'GitLab token', re: /\bglpat-[A-Za-z0-9_-]{20,}\b/g },
22
+ { kind: 'Slack token', re: /\bxox[abposr]-[0-9]{6,}-[0-9A-Za-z-]{10,}/g },
23
+ { kind: 'Slack webhook', re: /hooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{20,}/g },
24
+ { kind: 'Stripe live key', re: /\b[rs]k_live_[A-Za-z0-9]{20,}\b/g },
25
+ { kind: 'OpenAI API key', re: /\bsk-(?:proj-|svcacct-|admin-)?[A-Za-z0-9_-]{16,}T3BlbkFJ[A-Za-z0-9_-]{16,}/g },
26
+ { kind: 'Anthropic API key', re: /\bsk-ant-(?:api|admin|oat)\d{2}-[A-Za-z0-9_-]{60,}/g },
27
+ { kind: 'npm token', re: /\bnpm_[A-Za-z0-9]{36}\b/g },
28
+ { kind: 'Hugging Face token', re: /\bhf_[A-Za-z0-9]{30,40}\b/g },
29
+ { kind: 'Groq API key', re: /\bgsk_[A-Za-z0-9]{40,60}\b/g },
30
+ { kind: 'SendGrid API key', re: /\bSG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b/g },
31
+ { kind: 'DigitalOcean token', re: /\bdo[opr]_v1_[a-f0-9]{64}\b/g },
32
+ // .npmrc and friends: a registry password or token written out literally. `${NPM_TOKEN}` is the
33
+ // documented way to keep one out of the file, so a reference to the environment is not a secret.
34
+ { kind: 'registry credential', re: /(?:^|\n)[ \t]*(?:\/\/[^\s=]+:)?_(?:authToken|auth|password)[ \t]*=[ \t]*(?!\$\{)[^\s'"]{8,}/g },
35
+ ]
36
+
37
+ // Documentation and tests are full of keys that look real and are not: AWS's own example id,
38
+ // and placeholders like sk_live_XXXXXXXXXXXXXXXXXXXXXXXX. A provider's key is random, so a match
39
+ // with few distinct characters, or that says it is an example, is a placeholder.
40
+ const PLACEHOLDER = /EXAMPLE|example|XXXX|xxxx|0{8}|1234567890|your[_-]?(?:key|token|secret)/i
41
+ function placeholder(value, kind) {
42
+ if (kind === 'private key' || kind === 'registry credential') return false
43
+ if (PLACEHOLDER.test(value)) return true
44
+ return new Set(value).size < 10
45
+ }
46
+
47
+ // A SIGNED ACCESS TOKEN -- a JWT, which is what Hivecode's own room tokens are -- is a person: whoever
48
+ // holds it joins as them, with their access. It is kept out of the room's chat and tasks
49
+ // ({ tokens: true }). It is NOT a reason to hold a FILE back: auth code and its tests are full of
50
+ // sample tokens, most of them expired or fake, and holding each such file would stop real work.
51
+ export const TOKEN_PATTERNS = [
52
+ { kind: 'access token', re: /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{16,}/g },
53
+ ]
54
+
55
+ /** Every credential in `text`: [{ kind, line, value }]. `value` is for comparing, never for showing.
56
+ * `tokens: true` adds signed access tokens, for what goes into the room's chat and tasks. */
57
+ export function findSecrets(text, { tokens = false } = {}) {
58
+ const s = String(text || '')
59
+ if (!s) return []
60
+ const out = []
61
+ for (const { kind, re } of tokens ? [...PATTERNS, ...TOKEN_PATTERNS] : PATTERNS) {
62
+ re.lastIndex = 0
63
+ let m
64
+ while ((m = re.exec(s)) !== null) {
65
+ const value = m[0].replace(/^\s+/, '')
66
+ if (!placeholder(value, kind)) {
67
+ const at = m.index + (m[0].length - m[0].replace(/^\s+/, '').length)
68
+ out.push({ kind, line: s.slice(0, at).split('\n').length, value })
69
+ }
70
+ if (m[0].length === 0) re.lastIndex++
71
+ }
72
+ }
73
+ return out.sort((a, b) => a.line - b.line)
74
+ }
75
+
76
+ /** Credentials in `local` that `shared` does not already contain: what sharing `local` would leak. */
77
+ export function newSecrets(local, shared) {
78
+ const found = findSecrets(local)
79
+ if (!found.length) return []
80
+ const room = String(shared || '')
81
+ return found.filter((f) => !room.includes(f.value))
82
+ }
83
+
84
+ /** "an OpenAI API key (line 12)" -- the only form a finding is ever shown in. */
85
+ export function describeSecret(f) {
86
+ if (!f) return ''
87
+ const a = /^(?:[aeiou]|npm\b)/i.test(f.kind) ? 'an' : 'a'
88
+ return `${a} ${f.kind} (line ${f.line})`
89
+ }
@@ -0,0 +1,114 @@
1
+ // hive-session.js -- which agent this is, on disk, for the folder its app started it in.
2
+ //
3
+ // The MCP server records its session in <launch folder>/.hive-mcp-session.json, so a restart rejoins
4
+ // as the same agent; the Stop hook, which runs in the same launch folder, reads it to learn which
5
+ // agent it is guarding.
6
+ //
7
+ // WHY THIS IS A MODULE. Until 9 Oct 2026 that file held ONE agent. Two agents started from one folder
8
+ // -- two Claude Code sessions in one repo, the obvious way to run a branch room -- overwrote each
9
+ // other, and the last writer was the only agent on record. The other one's Stop hook then steered it
10
+ // as the wrong agent (that agent's mail, a listener under that agent's name), and a restart brought
11
+ // it back as the other one. Found by walking the founder flow end to end (branch -> two agents ->
12
+ // pr), where the file also showed up in the developer's `git status`.
13
+ //
14
+ // Now the file keeps every agent started in that folder, each with the pid of the server that wrote
15
+ // it. Its top level is still the latest writer, which is all a Stop hook from before this reads, so an
16
+ // older hook keeps working exactly as it did. Server and hook read it through this one module, so
17
+ // the two cannot drift apart (the merge logic once lived twice, and the copies did).
18
+ import fs from 'fs'
19
+ import path from 'path'
20
+ import { spawnSync } from 'child_process'
21
+
22
+ export const SESSION_FILE_NAME = '.hive-mcp-session.json'
23
+ export const HOOK_STATE_NAME = '.hive-stop-hook.json'
24
+ const STALE_MS = 14 * 24 * 3600 * 1000 // a record whose server is gone and that nobody wrote for two weeks
25
+
26
+ export function pidAlive(pid) {
27
+ if (!Number.isInteger(pid) || pid <= 0) return false
28
+ try { process.kill(pid, 0); return true } catch (e) { return !!e && e.code === 'EPERM' }
29
+ }
30
+
31
+ /** Every agent on record in a session file, by name. A file from before 9 Oct holds one, at its top level. */
32
+ export function readAgents(file) {
33
+ let raw
34
+ try { raw = JSON.parse(fs.readFileSync(file, 'utf8')) } catch { return {} }
35
+ if (!raw || typeof raw !== 'object') return {}
36
+ if (raw.agents && typeof raw.agents === 'object') return { ...raw.agents }
37
+ if (raw.name) { const one = { ...raw }; delete one.agents; return { [raw.name]: one } }
38
+ return {}
39
+ }
40
+
41
+ /** Write the records back; the top level is `latest` (or the newest record), for a Stop hook from before 9 Oct. */
42
+ export function writeAgents(file, agents, latest = null) {
43
+ const now = Date.now()
44
+ for (const [n, s] of Object.entries(agents)) if (!pidAlive(s.pid) && s.at && now - s.at > STALE_MS) delete agents[n]
45
+ const names = Object.keys(agents)
46
+ if (!names.length) { try { fs.rmSync(file) } catch { } return }
47
+ const top = latest && agents[latest.name] === latest ? latest
48
+ : agents[names.sort((a, b) => (agents[b].at || 0) - (agents[a].at || 0))[0]]
49
+ fs.writeFileSync(file, JSON.stringify({ ...top, agents }))
50
+ }
51
+
52
+ /** The record this server process owns: the one it wrote; else the one for its conversation; else, after
53
+ * a restart, the only one whose server is gone. Two gone is a guess, and a guess here is exactly how one
54
+ * agent wakes up as another, so it answers null and the agent rejoins by name instead. */
55
+ export function ownRecord(agents, { pid = process.pid, conv = '' } = {}) {
56
+ const all = Object.values(agents)
57
+ const mine = all.find((s) => s.pid === pid) || (conv ? all.find((s) => s.conv === conv) : null)
58
+ if (mine) return mine
59
+ const gone = all.filter((s) => !pidAlive(s.pid))
60
+ return gone.length === 1 ? gone[0] : null
61
+ }
62
+
63
+ /** Which agent a Stop hook is guarding. One on record is that one. With several, the hook's own
64
+ * conversation decides: its id, else its transcript (the hive_join that answered "folder: <dir>",
65
+ * then that folder anywhere in it), else the only one whose server still runs. If none of that
66
+ * tells, null: a hook that speaks as the wrong agent does more harm than one that stays quiet. */
67
+ export function agentForHook(agents, hook = {}, { readTail = tailOf } = {}) {
68
+ const all = Object.values(agents).filter((s) => s && s.dir && s.name)
69
+ if (all.length <= 1) return all[0] || null
70
+ const conv = String(hook.session_id || hook.conversationId || '')
71
+ if (conv) { const byConv = all.find((s) => s.conv && s.conv === conv); if (byConv) return byConv }
72
+ const t = hook.transcript_path ? readTail(hook.transcript_path) : ''
73
+ if (t) {
74
+ // The folder as a transcript line spells it: escaped once as JSON, twice if the text was nested.
75
+ const spell = (dir) => { const once = JSON.stringify(String(dir)).slice(1, -1); return [once, JSON.stringify(once).slice(1, -1)] }
76
+ const where = (s) => {
77
+ const forms = spell(s.dir)
78
+ const joined = Math.max(...forms.map((f) => t.lastIndexOf('folder: ' + f)))
79
+ return joined >= 0 ? [1, joined] : [0, Math.max(...forms.map((f) => t.lastIndexOf(f)))]
80
+ }
81
+ const ranked = all.map((s) => ({ s, w: where(s) })).filter((x) => x.w[1] >= 0)
82
+ .sort((a, b) => b.w[0] - a.w[0] || b.w[1] - a.w[1])
83
+ if (ranked.length) return ranked[0].s
84
+ }
85
+ const live = all.filter((s) => pidAlive(s.pid))
86
+ return live.length === 1 ? live[0] : null
87
+ }
88
+
89
+ function tailOf(file, max = 8 * 1024 * 1024) {
90
+ try {
91
+ const size = fs.statSync(file).size
92
+ const n = Math.min(size, max)
93
+ const fd = fs.openSync(file, 'r')
94
+ try { const b = Buffer.alloc(n); fs.readSync(fd, b, 0, n, size - n); return b.toString('utf8') } finally { fs.closeSync(fd) }
95
+ } catch { return '' }
96
+ }
97
+
98
+ /** Keep our files out of `git status` in the launch folder, which is often the developer's own repo:
99
+ * listed in that repo's local exclude file (.git/info/exclude), never in a tracked .gitignore. */
100
+ export function excludeFromGit(dir, names = [SESSION_FILE_NAME, HOOK_STATE_NAME]) {
101
+ try {
102
+ const r = spawnSync('git', ['rev-parse', '--git-path', 'info/exclude'], { cwd: dir, encoding: 'utf8', windowsHide: true, timeout: 5000 })
103
+ if (r.status !== 0 || !String(r.stdout || '').trim()) return false
104
+ const file = path.resolve(dir, String(r.stdout).trim())
105
+ let have = ''
106
+ try { have = fs.readFileSync(file, 'utf8') } catch { }
107
+ const listed = new Set(have.split(/\r?\n/).map((l) => l.trim()))
108
+ const add = names.filter((n) => !listed.has(n) && !listed.has('/' + n))
109
+ if (!add.length) return true
110
+ fs.mkdirSync(path.dirname(file), { recursive: true })
111
+ fs.appendFileSync(file, `${have && !have.endsWith('\n') ? '\n' : ''}# Hivecode's local session files\n${add.join('\n')}\n`)
112
+ return true
113
+ } catch { return false }
114
+ }