murmur-it 0.2.9 → 0.2.11
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 +59 -1
- package/adapters/hummin/murmur.ts +86 -4
- package/adapters/opencode/murmur.ts +78 -3
- package/dist/broker.js +48 -15
- package/dist/cli-identity.js +25 -0
- package/dist/cli.js +2 -22
- package/dist/core.js +215 -15
- package/dist/hooks.js +4 -3
- package/dist/machine.js +205 -0
- package/dist/mcp.js +54 -14
- package/dist/protocol.js +4 -0
- package/dist/store.js +44 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,62 @@ All notable changes to murmur are documented here. The format follows [Keep a Ch
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.2.11] - 2026-11-17
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- A session of another machine no longer looks dead to the broker, and the sessions of a machine that rebooted no longer keep their handles.
|
|
12
|
+
|
|
13
|
+
**What changed**
|
|
14
|
+
|
|
15
|
+
- Every host reference (pid and start time) carries the machine it runs on: `machine`, its boot id, and `machineName`, its host name, only as a label and never compared for identity. The boot id is `/proc/sys/kernel/random/boot_id` plus `:` and the pid namespace number (`/proc/self/ns/pid`) on Linux, so a container is another machine (the bare boot id where the link cannot be read), and `/usr/sbin/sysctl -n kern.bootsessionuuid` with a one second limit on macOS.
|
|
16
|
+
- The broker takes a machine only as a uuid, optionally followed by `:` and 1 to 20 digits, and keeps it in lower case; a name only as 1 to 255 printable characters. Anything else reads as absent.
|
|
17
|
+
- A session of the broker's own machine is judged by the process table as before. A session of another machine is alive while a connection of its host (pid, start, machine) is open, for a lease of 12 hours after its last session event (SessionStart, a prompt, a stop; SessionEnd ends it at once), or for 60 seconds after the broker starts, while the MCP servers reconnect (20 tries, 250 ms apart: `RECONNECT_ATTEMPTS` and `RECONNECT_DELAY_MS` in `src/mcp.ts`). Receiving messages does not renew the lease; the connection does.
|
|
18
|
+
- Two sessions in one folder on two machines get distinct handles (`claude-guard`, `claude-guard-2`) and show as two peers. A title cannot take the handle of a live session of another machine. A pid and start that collide across machines or boots do not match. A crashed session of this machine is no longer a sibling: restarting in its folder and naming the new session carries the groups.
|
|
19
|
+
- A session that comes back after another took its handle (an ended row revived by its next event) never shares it: it takes the next free handle, and the queue and groups stay with the holder.
|
|
20
|
+
- A session event also carries `machineFingerprint` (the first 32 hex characters of sha256("murmur-machine:" + the id the system keeps for itself): `/etc/machine-id`, else `/var/lib/dbus/machine-id`, on Linux; the IOPlatformUUID of `/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice` on macOS) and `machineUptime` (seconds since boot, from `/proc/uptime` or `kern.boottime`). The raw id never leaves the machine, and no client is shown a fingerprint. An id that is not 32 lower case hex characters (a systemd placeholder such as `uninitialized`, an id baked into an image) gives no fingerprint, so such a machine ends nothing and the lease applies; the opencode and hummin adapters carry the same check, and the fingerprint is not even read when there is no boot id.
|
|
21
|
+
- A reboot is a new boot id for the same fingerprint and the same host name. The old sessions of that machine end at once when they were last heard from before the new boot (the uptime), none of their hosts is connected, and the broker is past its 60 second startup grace. A resumed session with the same CLI id keeps its row.
|
|
22
|
+
- A host name alone is no evidence: two machines can share one (`raspberrypi`, `localhost`, two Macs called `Mac`), have different fingerprints, and are never read as each other's reboot. A quiet one keeps its handle, groups, queue and native transport.
|
|
23
|
+
- Nothing ends when the fingerprint is missing on the event or on the row, when the host name changed, when there is no uptime, or while the old host is connected: the lease applies.
|
|
24
|
+
- An event without a machine (a boot id read that failed this once) leaves the machine and label its session had. An event with a machine but no label or fingerprint keeps those of the same machine.
|
|
25
|
+
- A client that gains a machine id, or whose boot id read failed once, finds the row of its own process only when the CLI ids do not conflict, when the folders are equal if the event and the row both carry one, when no live process of the broker's machine has that pid and start, and when the candidates are of one machine: an event without a machine whose pid and start match rows of two machines takes neither. The lookup an MCP server makes by its host applies the same tests as the event it belongs to, so a lookup adopts no row that the event itself would not.
|
|
26
|
+
- A broker that could not read its own boot id at start tries again every minute. Once it can, the rows clients wrote under that id while it was read as another machine become its own (machine, label and fingerprint cleared) and the process table judges them from then on.
|
|
27
|
+
- The hooks, the MCP server, the CLI and the opencode and hummin adapters send the fields, each reading them once per process (a hook is a new process per event, so a read that failed yields a sparse event, handled as above).
|
|
28
|
+
- Sessions are matched by pid, start and machine (`host_machine`, `host_label` and `host_fingerprint`, nullable columns added to an existing database when it opens; NULL is the broker's own machine). The fields are additive and the broker never checks `v`, so a new client works against an old broker, which ignores them.
|
|
29
|
+
|
|
30
|
+
**Limits**
|
|
31
|
+
|
|
32
|
+
- A crashed session of another machine keeps its handle for the 12 hour lease; so does one of the broker's own machine that sends a boot id while the broker cannot read its own. A container that restarts gets a new pid namespace, so its old session is another machine's and holds its handle for the lease.
|
|
33
|
+
- Two clones of one image share a machine id and a host name: the connected one stays and a hook-only one ends. Remedy: `systemd-machine-id-setup`, or distinct host names.
|
|
34
|
+
- Any client on the bus can claim any machine and keep a handle alive by renewing a lease. It can end a connection-less session of another machine only by presenting that machine's fingerprint and host name with a new boot id and an uptime that predates the session's last event. The token is still the trust boundary: whoever holds it is on the bus.
|
|
35
|
+
- An upgraded hook adopts the row of its old process while that process's MCP server is connected. A clear or a resume that mints a new CLI id in that window, or a change of folder, opens a second row. The case left is an old row without a CLI id, with the same pid and the same `ps lstart` second on two machines started from one image, when the two folders are equal or when the event or the row carries none (a missing folder is not a difference).
|
|
36
|
+
- A hook and an MCP server of one session that run in different pid namespaces (a wrapper that starts the hook under `unshare` or `bwrap`), or where only one of them can read `/proc/self/ns/pid`, send different machine ids. When the host process is alive in the broker's process table, the session splits into two rows with two handles (`claude-guard` and `claude-guard-2`). Remedy: run the hooks in the pid namespace of the CLI.
|
|
37
|
+
- Two containers are one machine, and colliding pids one session, only when they share one pid namespace (or cannot read the link, so both send the bare boot id). A container with its own pid namespace is another machine, as above. Containers of one image that share a host name and a machine id and show a per container uptime (lxcfs) look like reboots of each other: the older one's connection-less sessions end. Give each container its own host name.
|
|
38
|
+
- A client that predates the fields sends none and is judged by the process table, so a session of another machine that runs an old client still looks dead, shares a handle with a new session and is carried along by a rename again (the fix below protects only sessions the broker sees alive).
|
|
39
|
+
|
|
40
|
+
**Upgrade steps**
|
|
41
|
+
|
|
42
|
+
1. Update murmur on the broker's machine and on every machine whose sessions reach it, then restart their running sessions: a hook that sends the machine next to an MCP server that does not splits one session in two until it restarts.
|
|
43
|
+
2. Nothing to migrate: an existing database gains the columns when it opens.
|
|
44
|
+
3. Give each clone of an image its own machine id (`systemd-machine-id-setup`) or its own host name.
|
|
45
|
+
4. On macOS, check that `/usr/sbin/sysctl -n kern.bootsessionuuid` and `/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice` print a uuid and an `IOPlatformUUID` line.
|
|
46
|
+
|
|
47
|
+
- The native transport of a handle two sessions hold is no longer replaced by the second session's registration, and the first `SessionEnd` no longer deletes it.
|
|
48
|
+
|
|
49
|
+
- Naming a session no longer carries other sessions along. The broker judges a session alive from its own process table, so a session of another machine looks dead and its handle reads free: a second live session was given the same handle, and when one of them took its title (or was renamed with `murmur_name`) the whole identity was renamed: every session on the old handle moved, their earlier messages changed sender, and the MCP connections of the others followed. While another session that has not ended holds the old handle, only the renamed session moves, the others keep their handle, history, groups and native transport, and only the renamed session's own MCP connection follows. A lone session renaming itself still carries its history, groups and transport, and so does an ended sibling that held the same handle.
|
|
50
|
+
|
|
51
|
+
The consequence, plainly: after a rename away from a shared handle the renamed session starts with no history, no group memberships, no role and an empty inbox. Everything recorded under the old handle (messages, queued deliveries, groups, the role, the native transport) stays with the session that keeps it, because nothing records which session wrote an old message; replies to a message the renamed session sent before the split go to the session that stayed. A connection that names no host (a CLI or adapter connection bound by handle) also stays with the old handle. A sibling is a session that is alive (see the machine entry above): a session of this machine that crashed without a SessionEnd is not, so restarting in its folder and naming the new session carries the groups; a client that sends no machine and runs on another machine still counts as dead.
|
|
52
|
+
|
|
53
|
+
## [0.2.10] - 2026-11-05
|
|
54
|
+
|
|
55
|
+
### Added
|
|
56
|
+
|
|
57
|
+
- Pushes reach sessions on another machine. In a remote hub home the MCP server registers with `listen: true` and hands each push and notice to its own session through `CLAUDE_CODE_MESSAGING_SOCKET`, which the hub cannot reach across machines; a push the socket took is confirmed with `delivered`. An idle session there now wakes on a message instead of waiting for its next prompt. Protocol: `register` takes `listen` for `mcp` and `adapter` connections, and a listening registration gets its queued push messages right away.
|
|
58
|
+
|
|
59
|
+
### Fixed
|
|
60
|
+
|
|
61
|
+
- Sessions on another machine count as alive. A session counts as alive when its host process runs on the hub or when a live `mcp` or `adapter` connection from its host is open. Before, the hub looked only at its own process table, so a session on another machine showed as `presumed-offline` and two of them in folders with the same name could get the same handle.
|
|
62
|
+
|
|
7
63
|
## [0.2.9] - 2026-11-05
|
|
8
64
|
|
|
9
65
|
### Added
|
|
@@ -171,7 +227,9 @@ All notable changes to murmur are documented here. The format follows [Keep a Ch
|
|
|
171
227
|
- Limits: send rate, duplicate window, thread and inbox caps.
|
|
172
228
|
- 283 tests at 100 percent lines and branches, and an end-to-end rig that replayed the live 3-peer scenario.
|
|
173
229
|
|
|
174
|
-
[Unreleased]: https://github.com/rennf93/murmur/compare/v0.2.
|
|
230
|
+
[Unreleased]: https://github.com/rennf93/murmur/compare/v0.2.11...HEAD
|
|
231
|
+
[0.2.11]: https://github.com/rennf93/murmur/releases/tag/v0.2.11
|
|
232
|
+
[0.2.10]: https://github.com/rennf93/murmur/releases/tag/v0.2.10
|
|
175
233
|
[0.2.9]: https://github.com/rennf93/murmur/releases/tag/v0.2.9
|
|
176
234
|
[0.2.8]: https://github.com/rennf93/murmur/releases/tag/v0.2.8
|
|
177
235
|
[0.2.7]: https://github.com/rennf93/murmur/releases/tag/v0.2.7
|
|
@@ -23,9 +23,10 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import { execFileSync } from "node:child_process";
|
|
26
|
-
import {
|
|
26
|
+
import { createHash } from "node:crypto";
|
|
27
|
+
import { readFileSync, readlinkSync } from "node:fs";
|
|
27
28
|
import { connect, type Socket } from "node:net";
|
|
28
|
-
import { homedir } from "node:os";
|
|
29
|
+
import { homedir, hostname } from "node:os";
|
|
29
30
|
import { join } from "node:path";
|
|
30
31
|
import { Type } from "typebox";
|
|
31
32
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
@@ -41,7 +42,7 @@ const RECONNECT_MS = 5000;
|
|
|
41
42
|
const REQUEST_TIMEOUT_MS = 30_000;
|
|
42
43
|
|
|
43
44
|
type Json = Record<string, unknown>;
|
|
44
|
-
interface Host { hostPid: number; hostStart: string }
|
|
45
|
+
interface Host { hostPid: number; hostStart: string; machine?: string; machineName?: string; machineFingerprint?: string; readonly machineUptime?: number }
|
|
45
46
|
|
|
46
47
|
// ---------------------------------------------------------------------------
|
|
47
48
|
// socket client (request/reply by id, events to a callback)
|
|
@@ -139,6 +140,8 @@ function openClient(onEvent: (f: Json) => void, onClose: () => void): Promise<Cl
|
|
|
139
140
|
});
|
|
140
141
|
}
|
|
141
142
|
|
|
143
|
+
let machineOnce: { machine?: string; machineName?: string } | undefined;
|
|
144
|
+
|
|
142
145
|
function readHost(): Host {
|
|
143
146
|
// hostStart must match what the broker computes: ps lstart under LC_ALL=C
|
|
144
147
|
let hostStart = "";
|
|
@@ -150,7 +153,86 @@ function readHost(): Host {
|
|
|
150
153
|
} catch {
|
|
151
154
|
// broker will treat the host as unverifiable
|
|
152
155
|
}
|
|
153
|
-
|
|
156
|
+
// Read once per process: a later failing read must not change what this process said before.
|
|
157
|
+
machineOnce ??= machineRef();
|
|
158
|
+
const machine = machineOnce;
|
|
159
|
+
return {
|
|
160
|
+
hostPid: process.pid,
|
|
161
|
+
hostStart,
|
|
162
|
+
...machine,
|
|
163
|
+
// Read when a message is serialized, so the broker sees the uptime of that moment.
|
|
164
|
+
get machineUptime(): number | undefined {
|
|
165
|
+
return machine.machine === undefined ? undefined : machineUptime();
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Seconds since this machine booted (src/machine.ts readUptime).
|
|
171
|
+
function machineUptime(): number | undefined {
|
|
172
|
+
try {
|
|
173
|
+
if (process.platform === "linux") {
|
|
174
|
+
const seconds = Number.parseFloat(readFileSync("/proc/uptime", "utf8"));
|
|
175
|
+
return Number.isFinite(seconds) && seconds >= 0 ? Math.floor(seconds) : undefined;
|
|
176
|
+
}
|
|
177
|
+
if (process.platform === "darwin") {
|
|
178
|
+
const out = execFileSync("/usr/sbin/sysctl", ["-n", "kern.boottime"], { encoding: "utf8", timeout: 1000 });
|
|
179
|
+
const booted = /sec\s*=\s*(\d+)/.exec(out)?.[1];
|
|
180
|
+
return booted === undefined ? undefined : Math.max(0, Math.floor(Date.now() / 1000) - Number(booted));
|
|
181
|
+
}
|
|
182
|
+
} catch {
|
|
183
|
+
// no uptime
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// What the broker keeps with a host (src/machine.ts): the boot id, and the host
|
|
189
|
+
// name as a label; neither when there is no boot id, so the broker reads this
|
|
190
|
+
// as an old client of its own machine.
|
|
191
|
+
// A hash that names the machine across reboots (src/machine.ts readFingerprint):
|
|
192
|
+
// the raw system id never leaves the machine.
|
|
193
|
+
function machineFingerprint(): string | undefined {
|
|
194
|
+
const attempt = (read: () => string): string => {
|
|
195
|
+
try {
|
|
196
|
+
return read();
|
|
197
|
+
} catch {
|
|
198
|
+
return "";
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
let raw = "";
|
|
202
|
+
if (process.platform === "linux") {
|
|
203
|
+
raw = attempt(() => readFileSync("/etc/machine-id", "utf8").trim());
|
|
204
|
+
raw = /^[0-9a-f]{32}$/.test(raw) ? raw : attempt(() => readFileSync("/var/lib/dbus/machine-id", "utf8").trim());
|
|
205
|
+
raw = /^[0-9a-f]{32}$/.test(raw) ? raw : "";
|
|
206
|
+
} else if (process.platform === "darwin") {
|
|
207
|
+
raw = attempt(() => /"IOPlatformUUID"\s*=\s*"([0-9a-f-]{36})"/i.exec(execFileSync("/usr/sbin/ioreg", ["-rd1", "-c", "IOPlatformExpertDevice"], { encoding: "utf8", timeout: 1000 }))?.[1] ?? "");
|
|
208
|
+
}
|
|
209
|
+
return raw === "" ? undefined : createHash("sha256").update("murmur-machine:" + raw).digest("hex").slice(0, 32);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function machineRef(): { machine?: string; machineName?: string; machineFingerprint?: string } {
|
|
213
|
+
try {
|
|
214
|
+
const raw =
|
|
215
|
+
process.platform === "linux"
|
|
216
|
+
? readFileSync("/proc/sys/kernel/random/boot_id", "utf8")
|
|
217
|
+
: process.platform === "darwin"
|
|
218
|
+
? execFileSync("/usr/sbin/sysctl", ["-n", "kern.bootsessionuuid"], { encoding: "utf8", timeout: 1000 })
|
|
219
|
+
: "";
|
|
220
|
+
const uuid = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i.exec(raw);
|
|
221
|
+
if (uuid !== null) {
|
|
222
|
+
// On Linux the pid namespace is part of the machine: a process table is one machine's.
|
|
223
|
+
let namespace = "";
|
|
224
|
+
try {
|
|
225
|
+
namespace = process.platform === "linux" ? (/\[(\d+)\]/.exec(readlinkSync("/proc/self/ns/pid"))?.[1] ?? "") : "";
|
|
226
|
+
} catch {
|
|
227
|
+
// no namespace to read
|
|
228
|
+
}
|
|
229
|
+
const fingerprint = machineFingerprint();
|
|
230
|
+
return { machine: uuid[0].toLowerCase() + (namespace === "" ? "" : ":" + namespace), machineName: hostname(), ...(fingerprint !== undefined ? { machineFingerprint: fingerprint } : {}) };
|
|
231
|
+
}
|
|
232
|
+
} catch {
|
|
233
|
+
// no boot id
|
|
234
|
+
}
|
|
235
|
+
return {};
|
|
154
236
|
}
|
|
155
237
|
|
|
156
238
|
function text(s: string, isError = false): { content: { type: "text"; text: string }[]; details: undefined; isError?: boolean } {
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
// and the murmur_* tools talk over the calling session's connection.
|
|
4
4
|
import { tool, type Plugin } from '@opencode-ai/plugin';
|
|
5
5
|
import { execFileSync } from 'node:child_process';
|
|
6
|
-
import {
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import { readFileSync, readlinkSync } from 'node:fs';
|
|
7
8
|
import { connect, type Socket } from 'node:net';
|
|
8
|
-
import { homedir } from 'node:os';
|
|
9
|
+
import { homedir, hostname } from 'node:os';
|
|
9
10
|
import { join } from 'node:path';
|
|
10
11
|
|
|
11
12
|
const HOME = process.env.MURMUR_HOME || join(homedir(), '.murmur');
|
|
@@ -20,7 +21,81 @@ try {
|
|
|
20
21
|
} catch {
|
|
21
22
|
// leave empty; the broker will refuse the announce and we retry quietly
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
+
// What the broker keeps with a host (src/machine.ts): the boot id, and the host
|
|
25
|
+
// name as a label; neither when there is no boot id, so the broker reads this
|
|
26
|
+
// as an old client of its own machine.
|
|
27
|
+
// A hash that names the machine across reboots (src/machine.ts readFingerprint):
|
|
28
|
+
// the raw system id never leaves the machine.
|
|
29
|
+
function machineFingerprint(): string | undefined {
|
|
30
|
+
const attempt = (read: () => string): string => {
|
|
31
|
+
try {
|
|
32
|
+
return read();
|
|
33
|
+
} catch {
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
let raw = '';
|
|
38
|
+
if (process.platform === 'linux') {
|
|
39
|
+
raw = attempt(() => readFileSync('/etc/machine-id', 'utf8').trim());
|
|
40
|
+
raw = /^[0-9a-f]{32}$/.test(raw) ? raw : attempt(() => readFileSync('/var/lib/dbus/machine-id', 'utf8').trim());
|
|
41
|
+
raw = /^[0-9a-f]{32}$/.test(raw) ? raw : '';
|
|
42
|
+
} else if (process.platform === 'darwin') {
|
|
43
|
+
raw = attempt(() => /"IOPlatformUUID"\s*=\s*"([0-9a-f-]{36})"/i.exec(execFileSync('/usr/sbin/ioreg', ['-rd1', '-c', 'IOPlatformExpertDevice'], { encoding: 'utf8', timeout: 1000 }))?.[1] ?? '');
|
|
44
|
+
}
|
|
45
|
+
return raw === '' ? undefined : createHash('sha256').update('murmur-machine:' + raw).digest('hex').slice(0, 32);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function machineRef(): { machine?: string; machineName?: string; machineFingerprint?: string } {
|
|
49
|
+
try {
|
|
50
|
+
const raw =
|
|
51
|
+
process.platform === 'linux'
|
|
52
|
+
? readFileSync('/proc/sys/kernel/random/boot_id', 'utf8')
|
|
53
|
+
: process.platform === 'darwin'
|
|
54
|
+
? execFileSync('/usr/sbin/sysctl', ['-n', 'kern.bootsessionuuid'], { encoding: 'utf8', timeout: 1000 })
|
|
55
|
+
: '';
|
|
56
|
+
const uuid = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i.exec(raw);
|
|
57
|
+
if (uuid !== null) {
|
|
58
|
+
// On Linux the pid namespace is part of the machine: a process table is one machine's.
|
|
59
|
+
let namespace = '';
|
|
60
|
+
try {
|
|
61
|
+
namespace = process.platform === 'linux' ? (/\[(\d+)\]/.exec(readlinkSync('/proc/self/ns/pid'))?.[1] ?? '') : '';
|
|
62
|
+
} catch {
|
|
63
|
+
// no namespace to read
|
|
64
|
+
}
|
|
65
|
+
const fingerprint = machineFingerprint();
|
|
66
|
+
return { machine: uuid[0].toLowerCase() + (namespace === '' ? '' : ':' + namespace), machineName: hostname(), ...(fingerprint !== undefined ? { machineFingerprint: fingerprint } : {}) };
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
// no boot id
|
|
70
|
+
}
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
// Seconds since this machine booted, read when a message is serialized (src/machine.ts readUptime).
|
|
74
|
+
function machineUptime(): number | undefined {
|
|
75
|
+
try {
|
|
76
|
+
if (process.platform === 'linux') {
|
|
77
|
+
const seconds = Number.parseFloat(readFileSync('/proc/uptime', 'utf8'));
|
|
78
|
+
return Number.isFinite(seconds) && seconds >= 0 ? Math.floor(seconds) : undefined;
|
|
79
|
+
}
|
|
80
|
+
if (process.platform === 'darwin') {
|
|
81
|
+
const out = execFileSync('/usr/sbin/sysctl', ['-n', 'kern.boottime'], { encoding: 'utf8', timeout: 1000 });
|
|
82
|
+
const booted = /sec\s*=\s*(\d+)/.exec(out)?.[1];
|
|
83
|
+
return booted === undefined ? undefined : Math.max(0, Math.floor(Date.now() / 1000) - Number(booted));
|
|
84
|
+
}
|
|
85
|
+
} catch {
|
|
86
|
+
// no uptime
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const machine = machineRef();
|
|
91
|
+
const host = {
|
|
92
|
+
hostPid: process.pid,
|
|
93
|
+
hostStart,
|
|
94
|
+
...machine,
|
|
95
|
+
get machineUptime(): number | undefined {
|
|
96
|
+
return machine.machine === undefined ? undefined : machineUptime();
|
|
97
|
+
},
|
|
98
|
+
};
|
|
24
99
|
|
|
25
100
|
type Pending = { resolve: (v: any) => void; reject: (e: Error) => void; timer: ReturnType<typeof setTimeout> };
|
|
26
101
|
|
package/dist/broker.js
CHANGED
|
@@ -2,11 +2,12 @@ import { chmodSync, existsSync, unlinkSync } from 'node:fs';
|
|
|
2
2
|
import { createConnection, createServer } from 'node:net';
|
|
3
3
|
import { StringDecoder } from 'node:string_decoder';
|
|
4
4
|
import { readToken } from './config.js';
|
|
5
|
+
import { parseWireMachine, validBootId } from './machine.js';
|
|
5
6
|
import { fullOf } from './core.js';
|
|
6
7
|
import { nativeSend } from './native.js';
|
|
7
8
|
import { ProofWatcher } from './proof.js';
|
|
8
9
|
import { defaultWake } from './wake.js';
|
|
9
|
-
import { LineReader, MurmurError, NOTIFY_BATCH_MS, PROTOCOL_VERSION, THREADS_DEFAULT, THREADS_MAX, WAIT_MAX_MS, encodeLine, renderDelivery, replyFooter, } from './protocol.js';
|
|
10
|
+
import { LineReader, MurmurError, NOTIFY_BATCH_MS, PROTOCOL_VERSION, THREADS_DEFAULT, THREADS_MAX, WAIT_MAX_MS, encodeLine, noticeText, renderDelivery, replyFooter, } from './protocol.js';
|
|
10
11
|
const CLIENT_KINDS = ['mcp', 'cli', 'hook', 'adapter'];
|
|
11
12
|
const HOOK_EVENTS = ['session-start', 'prompt', 'stop', 'session-end'];
|
|
12
13
|
const CLI_NAMES = ['claude', 'codex', 'gemini', 'zcode', 'opencode', 'hummin', 'other'];
|
|
@@ -100,7 +101,13 @@ function reqHost(value) {
|
|
|
100
101
|
throw badRequest('host must be an object');
|
|
101
102
|
}
|
|
102
103
|
const rec = value;
|
|
103
|
-
|
|
104
|
+
// A machine that is not a boot id in shape reads as absent, and the label, the
|
|
105
|
+
// uptime and the fingerprint mean something only next to one.
|
|
106
|
+
return {
|
|
107
|
+
hostPid: reqNumber(rec, 'hostPid'),
|
|
108
|
+
hostStart: reqString(rec, 'hostStart'),
|
|
109
|
+
...parseWireMachine(rec),
|
|
110
|
+
};
|
|
104
111
|
}
|
|
105
112
|
function reqEnum(rec, key, allowed) {
|
|
106
113
|
const value = rec[key];
|
|
@@ -142,6 +149,7 @@ export class Broker {
|
|
|
142
149
|
this.nativeSendFn = opts.nativeSend ?? nativeSend;
|
|
143
150
|
this.wakeFn = opts.wake ?? defaultWake;
|
|
144
151
|
this.core.hasTools = (handle) => this.presence.has(handle);
|
|
152
|
+
this.core.hostConnected = (host) => [...this.conns].some((c) => c.live && c.host !== null && this.atRow(c.host, host));
|
|
145
153
|
this.proof = new ProofWatcher((messageId, recipient) => this.onProof(messageId, recipient), opts.proofPollMs);
|
|
146
154
|
}
|
|
147
155
|
static async start(opts) {
|
|
@@ -227,6 +235,7 @@ export class Broker {
|
|
|
227
235
|
live: false,
|
|
228
236
|
listening: false,
|
|
229
237
|
pendingHost: null,
|
|
238
|
+
host: null,
|
|
230
239
|
pendingBytes: 0,
|
|
231
240
|
waiters: new Set(),
|
|
232
241
|
chain: Promise.resolve(),
|
|
@@ -361,7 +370,7 @@ export class Broker {
|
|
|
361
370
|
case 'auth':
|
|
362
371
|
throw badRequest('connection is already authenticated');
|
|
363
372
|
case 'register':
|
|
364
|
-
this.replyOk(conn.socket, id, this.handleRegister(conn, rec));
|
|
373
|
+
this.replyOk(conn.socket, id, await this.handleRegister(conn, rec));
|
|
365
374
|
return;
|
|
366
375
|
case 'hook':
|
|
367
376
|
this.replyOk(conn.socket, id, this.handleHook(conn, rec));
|
|
@@ -427,23 +436,32 @@ export class Broker {
|
|
|
427
436
|
}
|
|
428
437
|
// Handlers.
|
|
429
438
|
// See RegisterMessage for the identity rules.
|
|
430
|
-
handleRegister(conn, rec) {
|
|
439
|
+
async handleRegister(conn, rec) {
|
|
431
440
|
const kind = reqEnum(rec, 'clientKind', CLIENT_KINDS);
|
|
432
441
|
const handle = optString(rec, 'handle');
|
|
433
442
|
const display = optString(rec, 'display');
|
|
434
443
|
const role = optString(rec, 'role');
|
|
435
444
|
const host = rec.host === undefined ? undefined : reqHost(rec.host);
|
|
436
445
|
const ancestors = optNumberArray(rec, 'ancestors');
|
|
446
|
+
const machine = validBootId(rec.machine);
|
|
437
447
|
const client = optEnum(rec, 'client', CLI_NAMES) ?? 'other';
|
|
438
448
|
const cwd = optString(rec, 'cwd');
|
|
439
449
|
const owner = optBool(rec, 'owner') === true;
|
|
440
450
|
const live = kind === 'mcp' || kind === 'adapter';
|
|
441
451
|
let resolved;
|
|
442
452
|
conn.via = null;
|
|
453
|
+
// A rename registers again without these, so only a given value changes them.
|
|
454
|
+
const listen = optBool(rec, 'listen');
|
|
455
|
+
if (host !== undefined) {
|
|
456
|
+
conn.host = host;
|
|
457
|
+
}
|
|
458
|
+
if (live && listen !== undefined) {
|
|
459
|
+
conn.listening = listen;
|
|
460
|
+
}
|
|
443
461
|
if (owner) {
|
|
444
462
|
resolved = this.ownerHandle();
|
|
445
463
|
if (ancestors !== undefined) {
|
|
446
|
-
conn.via = this.core.sessionForAncestors(ancestors)?.handle ?? null;
|
|
464
|
+
conn.via = this.core.sessionForAncestors(ancestors, machine)?.handle ?? null;
|
|
447
465
|
}
|
|
448
466
|
}
|
|
449
467
|
else if (host !== undefined) {
|
|
@@ -458,10 +476,10 @@ export class Broker {
|
|
|
458
476
|
return { handle: '', owner: false };
|
|
459
477
|
}
|
|
460
478
|
resolved = handle === undefined ? session.handle : this.core.renameSession(session, handle);
|
|
461
|
-
this.rebind(session.handle, resolved);
|
|
479
|
+
this.rebind(session.handle, resolved, session);
|
|
462
480
|
}
|
|
463
481
|
else if (ancestors !== undefined && handle === undefined) {
|
|
464
|
-
resolved = this.core.sessionForAncestors(ancestors)?.handle ?? this.ownerHandle();
|
|
482
|
+
resolved = this.core.sessionForAncestors(ancestors, machine)?.handle ?? this.ownerHandle();
|
|
465
483
|
}
|
|
466
484
|
else if (handle !== undefined) {
|
|
467
485
|
resolved = this.core.register({ handle }).handle;
|
|
@@ -475,6 +493,10 @@ export class Broker {
|
|
|
475
493
|
}
|
|
476
494
|
this.bind(conn, resolved, live);
|
|
477
495
|
this.core.touchSeen(resolved);
|
|
496
|
+
if (live && listen === true) {
|
|
497
|
+
// A listener that comes (back) up gets what waited for it.
|
|
498
|
+
await this.pushQueued(resolved);
|
|
499
|
+
}
|
|
478
500
|
return { handle: resolved, owner: this.store.getPeer(resolved)?.isOwner === true };
|
|
479
501
|
}
|
|
480
502
|
ownerHandle() {
|
|
@@ -494,6 +516,14 @@ export class Broker {
|
|
|
494
516
|
this.bind(conn, handle, false);
|
|
495
517
|
return null;
|
|
496
518
|
}
|
|
519
|
+
// A connection's host reference against a stored session host: pid, start
|
|
520
|
+
// and machine (null is this broker's own machine).
|
|
521
|
+
atRow(ref, row) {
|
|
522
|
+
return ref.hostPid === row.hostPid && ref.hostStart === row.hostStart && this.core.machineKey(ref) === row.hostMachine;
|
|
523
|
+
}
|
|
524
|
+
sameRef(a, b) {
|
|
525
|
+
return a.hostPid === b.hostPid && a.hostStart === b.hostStart && this.core.machineKey(a) === this.core.machineKey(b);
|
|
526
|
+
}
|
|
497
527
|
async handleSession(conn, rec) {
|
|
498
528
|
const event = reqEnum(rec, 'event', HOOK_EVENTS);
|
|
499
529
|
const client = reqEnum(rec, 'client', CLI_NAMES);
|
|
@@ -513,7 +543,7 @@ export class Broker {
|
|
|
513
543
|
native = { socketPath: reqString(nat, 'socketPath'), token: reqString(nat, 'token') };
|
|
514
544
|
}
|
|
515
545
|
// A title can rename the session, so find the name it had before.
|
|
516
|
-
const before = (cliSessionId === null ? null : this.store.sessionByCliId(client, cliSessionId)) ?? this.
|
|
546
|
+
const before = (cliSessionId === null ? null : this.store.sessionByCliId(client, cliSessionId)) ?? this.core.sessionForHost(host, undefined, { cliSessionId, ...(cwd !== undefined ? { cwd } : {}) });
|
|
517
547
|
const session = this.core.sessionEvent({
|
|
518
548
|
event,
|
|
519
549
|
client,
|
|
@@ -526,14 +556,15 @@ export class Broker {
|
|
|
526
556
|
...(native !== undefined ? { native } : {}),
|
|
527
557
|
});
|
|
528
558
|
if (before !== null) {
|
|
529
|
-
this.rebind(before.handle, session.handle);
|
|
559
|
+
this.rebind(before.handle, session.handle, session);
|
|
530
560
|
}
|
|
531
561
|
conn.listening = listen;
|
|
562
|
+
conn.host = host;
|
|
532
563
|
this.bind(conn, session.handle, listen);
|
|
533
564
|
// MCP servers of this session that registered before it announced itself.
|
|
534
565
|
for (const other of this.conns) {
|
|
535
566
|
const pending = other.pendingHost;
|
|
536
|
-
if (pending !== null &&
|
|
567
|
+
if (pending !== null && this.sameRef(pending.host, host)) {
|
|
537
568
|
this.bind(other, session.handle, other.live);
|
|
538
569
|
}
|
|
539
570
|
}
|
|
@@ -916,8 +947,7 @@ export class Broker {
|
|
|
916
947
|
if (transport === null) {
|
|
917
948
|
return;
|
|
918
949
|
}
|
|
919
|
-
|
|
920
|
-
this.nativeSendFn(transport.socketPath, transport.token, text).catch(() => {
|
|
950
|
+
this.nativeSendFn(transport.socketPath, transport.token, noticeText(entry.payloads)).catch(() => {
|
|
921
951
|
// Notifications are best effort; the data stays in the DB.
|
|
922
952
|
});
|
|
923
953
|
}
|
|
@@ -1108,12 +1138,15 @@ export class Broker {
|
|
|
1108
1138
|
// A session's name changed: connections still bound to the old one (its MCP
|
|
1109
1139
|
// server, an adapter) follow it, unless another live session still holds
|
|
1110
1140
|
// it. Ended sessions left on the old name do not count.
|
|
1111
|
-
|
|
1112
|
-
|
|
1141
|
+
// While another session still holds the old name, only the connections of
|
|
1142
|
+
// the renamed session's own process follow.
|
|
1143
|
+
rebind(from, to, moved) {
|
|
1144
|
+
const shared = this.core.holdsHandle(from);
|
|
1145
|
+
if (from === to || (!shared && !this.core.claimable(from, null))) {
|
|
1113
1146
|
return;
|
|
1114
1147
|
}
|
|
1115
1148
|
for (const conn of this.conns) {
|
|
1116
|
-
if (conn.handle === from) {
|
|
1149
|
+
if (conn.handle === from && (!shared || (conn.host !== null && this.atRow(conn.host, moved)))) {
|
|
1117
1150
|
this.bind(conn, to, conn.live);
|
|
1118
1151
|
}
|
|
1119
1152
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { thisMachineRef } from './machine.js';
|
|
2
|
+
import { currentHost } from './proc.js';
|
|
3
|
+
// The register messages a shell command sends. Each says which machine its
|
|
4
|
+
// pids belong to (src/machine.ts), because the broker may run on another.
|
|
5
|
+
export const OWNER_DISPLAY = 'renn (owner)';
|
|
6
|
+
export function identityMessage(asHandle, speaker) {
|
|
7
|
+
if (asHandle !== null) {
|
|
8
|
+
return { type: 'register', handle: asHandle, clientKind: 'cli' };
|
|
9
|
+
}
|
|
10
|
+
if (speaker === 'owner') {
|
|
11
|
+
return {
|
|
12
|
+
type: 'register',
|
|
13
|
+
owner: true,
|
|
14
|
+
display: OWNER_DISPLAY,
|
|
15
|
+
clientKind: 'cli',
|
|
16
|
+
ancestors: currentHost()?.ancestors ?? [],
|
|
17
|
+
...thisMachineRef(),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return { type: 'register', clientKind: 'cli', ancestors: currentHost()?.ancestors ?? [], ...thisMachineRef() };
|
|
21
|
+
}
|
|
22
|
+
// The register that names the session of a host process, on this machine.
|
|
23
|
+
export function nameMessage(handle, host) {
|
|
24
|
+
return { type: 'register', clientKind: 'cli', handle, host: { hostPid: host.hostPid, hostStart: host.hostStart, ...thisMachineRef() } };
|
|
25
|
+
}
|
package/dist/cli.js
CHANGED
|
@@ -5,10 +5,10 @@ import { connectBroker, isBrokerDown } from './client.js';
|
|
|
5
5
|
import { ensureHome, getPaths, isRemoteHome, readToken } from './config.js';
|
|
6
6
|
import { HOOK_CLIENTS, runHook } from './hooks.js';
|
|
7
7
|
import { INSTALL_CLIENTS, installClient } from './install.js';
|
|
8
|
+
import { OWNER_DISPLAY, identityMessage, nameMessage } from './cli-identity.js';
|
|
8
9
|
import { currentHost } from './proc.js';
|
|
9
10
|
import { HANDLE_RE, MurmurError } from './protocol.js';
|
|
10
11
|
const OWNER_HANDLE = 'renn';
|
|
11
|
-
const OWNER_DISPLAY = 'renn (owner)';
|
|
12
12
|
const SEND_USAGE = 'usage: murmur send <targets> <body...> [--as <handle>] [--mode <mode>] [--refs <a,b>] [--thread <id>]';
|
|
13
13
|
const REPLY_USAGE = 'usage: murmur reply <message id> <body...> [--as <handle>] [--verdict agree|disagree|info|choice] [--refs <a,b>]';
|
|
14
14
|
const NAME_USAGE = 'usage: murmur name <handle> (renames the agent session this runs in)';
|
|
@@ -209,21 +209,6 @@ function readAllStdin() {
|
|
|
209
209
|
});
|
|
210
210
|
});
|
|
211
211
|
}
|
|
212
|
-
function identityMessage(asHandle, speaker) {
|
|
213
|
-
if (asHandle !== null) {
|
|
214
|
-
return { type: 'register', handle: asHandle, clientKind: 'cli' };
|
|
215
|
-
}
|
|
216
|
-
if (speaker === 'owner') {
|
|
217
|
-
return {
|
|
218
|
-
type: 'register',
|
|
219
|
-
owner: true,
|
|
220
|
-
display: OWNER_DISPLAY,
|
|
221
|
-
clientKind: 'cli',
|
|
222
|
-
ancestors: currentHost()?.ancestors ?? [],
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
return { type: 'register', clientKind: 'cli', ancestors: currentHost()?.ancestors ?? [] };
|
|
226
|
-
}
|
|
227
212
|
// Splits --flag value pairs from positionals. Null (after printing why)
|
|
228
213
|
// when a flag is missing its value.
|
|
229
214
|
function parseFlags(args, flags, usage, name, err) {
|
|
@@ -374,12 +359,7 @@ async function cmdName(args, out, err) {
|
|
|
374
359
|
}
|
|
375
360
|
const client = await connectCli();
|
|
376
361
|
try {
|
|
377
|
-
const msg =
|
|
378
|
-
type: 'register',
|
|
379
|
-
clientKind: 'cli',
|
|
380
|
-
handle,
|
|
381
|
-
host: { hostPid: host.hostPid, hostStart: host.hostStart },
|
|
382
|
-
};
|
|
362
|
+
const msg = nameMessage(handle, host);
|
|
383
363
|
const result = await client.request(msg);
|
|
384
364
|
out(result.handle + '\n');
|
|
385
365
|
return 0;
|