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/dist/mcp.js
CHANGED
|
@@ -3,9 +3,11 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
|
3
3
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { connectBroker } from './client.js';
|
|
6
|
-
import { getPaths, readToken } from './config.js';
|
|
6
|
+
import { getPaths, isRemoteHome, readToken } from './config.js';
|
|
7
|
+
import { nativeSend } from './native.js';
|
|
8
|
+
import { thisMachineRef } from './machine.js';
|
|
7
9
|
import { currentHost } from './proc.js';
|
|
8
|
-
import { HANDLE_RE, MurmurError, WAIT_DEFAULT_MS, WAIT_MAX_MS } from './protocol.js';
|
|
10
|
+
import { HANDLE_RE, MurmurError, WAIT_DEFAULT_MS, WAIT_MAX_MS, noticeText } from './protocol.js';
|
|
9
11
|
// MCP stdio server exposing the murmur broker to agent sessions. Every tool
|
|
10
12
|
// maps to one broker request on a single long-lived connection, and that
|
|
11
13
|
// connection is the session's presence on the bus. Identity comes from the
|
|
@@ -35,11 +37,11 @@ export function clientFromEnv(env) {
|
|
|
35
37
|
}
|
|
36
38
|
return 'other';
|
|
37
39
|
}
|
|
38
|
-
export function identityFor(host, env, cwd) {
|
|
40
|
+
export function identityFor(host, env, cwd, machine = thisMachineRef()) {
|
|
39
41
|
if (host === null) {
|
|
40
42
|
return {};
|
|
41
43
|
}
|
|
42
|
-
return { host: { hostPid: host.hostPid, hostStart: host.hostStart }, client: clientFromEnv(env), cwd };
|
|
44
|
+
return { host: { hostPid: host.hostPid, hostStart: host.hostStart, ...machine }, client: clientFromEnv(env), cwd };
|
|
43
45
|
}
|
|
44
46
|
export function buildMcpServer(client, identity = {}) {
|
|
45
47
|
const server = new McpServer({ name: 'murmur', version: VERSION }, { instructions: INSTRUCTIONS });
|
|
@@ -196,22 +198,59 @@ export function buildMcpServer(client, identity = {}) {
|
|
|
196
198
|
});
|
|
197
199
|
return server;
|
|
198
200
|
}
|
|
201
|
+
// This session's own messaging socket, when the home is a remote hub home:
|
|
202
|
+
// the hub cannot reach a socket on this machine, so this server listens for
|
|
203
|
+
// pushes and hands them over itself. Null otherwise.
|
|
204
|
+
export function pushTarget(env, home) {
|
|
205
|
+
const socketPath = env.CLAUDE_CODE_MESSAGING_SOCKET;
|
|
206
|
+
const token = env.CLAUDE_CODE_MESSAGING_TOKEN;
|
|
207
|
+
if (!isRemoteHome(home) || !socketPath || !token) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
return { socketPath, token };
|
|
211
|
+
}
|
|
212
|
+
// Hands one broker event to the session: a push through its messaging
|
|
213
|
+
// socket, confirmed as delivered once the socket took it (as an adapter
|
|
214
|
+
// confirms), or a notification batch as notice text.
|
|
215
|
+
// shortcut: a push the socket refuses stays sent (readable on the bus, never re-pushed), as with adapters; add a requeue message if refusals show up.
|
|
216
|
+
export async function deliverLocally(ev, target, client, send = nativeSend) {
|
|
217
|
+
if (ev.event === 'notification') {
|
|
218
|
+
await send(target.socketPath, target.token, noticeText(ev.payload));
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if ((await send(target.socketPath, target.token, ev.payload.text)).ok) {
|
|
222
|
+
const msg = { type: 'delivered', ids: [ev.payload.message.id] };
|
|
223
|
+
await client.request(msg).catch(() => {
|
|
224
|
+
// The broker connection went away: the push stays sent and readable on the bus.
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
199
228
|
// Retries after the broker connection drops: a restart or an upgrade takes a
|
|
200
229
|
// moment, and the murmur bin starts a broker itself when none answers.
|
|
201
230
|
const RECONNECT_ATTEMPTS = 20;
|
|
202
231
|
const RECONNECT_DELAY_MS = 250;
|
|
203
|
-
async function connect(paths) {
|
|
204
|
-
|
|
232
|
+
async function connect(paths, target) {
|
|
233
|
+
if (target === null) {
|
|
234
|
+
return connectBroker({ socketPath: paths.socketPath, token: readToken(paths) });
|
|
235
|
+
}
|
|
236
|
+
const client = await connectBroker({
|
|
237
|
+
socketPath: paths.socketPath,
|
|
238
|
+
token: readToken(paths),
|
|
239
|
+
onEvent: (ev) => {
|
|
240
|
+
void deliverLocally(ev, target, client);
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
return client;
|
|
205
244
|
}
|
|
206
245
|
// MURMUR_HANDLE is an optional per-process override; anything invalid
|
|
207
246
|
// (like an old CHANGE-ME placeholder) or already taken is ignored so the
|
|
208
247
|
// server still registers under its session's own handle.
|
|
209
|
-
async function register(client, identity) {
|
|
248
|
+
async function register(client, identity, listen) {
|
|
210
249
|
const explicit = process.env.MURMUR_HANDLE;
|
|
211
250
|
const wanted = explicit !== undefined && HANDLE_RE.test(explicit) ? explicit : undefined;
|
|
212
251
|
const base = identity.host === undefined
|
|
213
252
|
? { type: 'register', handle: wanted ?? 'anon-' + String(process.pid), clientKind: 'mcp' }
|
|
214
|
-
: { ...identity, type: 'register', clientKind: 'mcp' };
|
|
253
|
+
: { ...identity, type: 'register', clientKind: 'mcp', ...(listen ? { listen } : {}) };
|
|
215
254
|
try {
|
|
216
255
|
await client.request(wanted !== undefined && identity.host !== undefined ? { ...base, handle: wanted } : base);
|
|
217
256
|
}
|
|
@@ -225,7 +264,7 @@ async function register(client, identity) {
|
|
|
225
264
|
// A new registered connection for the same session, or null when no broker
|
|
226
265
|
// answered within the attempts or the server is going away meanwhile (a try
|
|
227
266
|
// may start a broker, which a session that is gone must not do).
|
|
228
|
-
async function reconnect(paths, identity, stopping) {
|
|
267
|
+
async function reconnect(paths, identity, target, stopping) {
|
|
229
268
|
for (let attempt = 0; attempt < RECONNECT_ATTEMPTS; attempt += 1) {
|
|
230
269
|
await new Promise((resolve) => setTimeout(resolve, RECONNECT_DELAY_MS));
|
|
231
270
|
if (stopping()) {
|
|
@@ -233,8 +272,8 @@ async function reconnect(paths, identity, stopping) {
|
|
|
233
272
|
}
|
|
234
273
|
let client;
|
|
235
274
|
try {
|
|
236
|
-
client = await connect(paths);
|
|
237
|
-
await register(client, identity);
|
|
275
|
+
client = await connect(paths, target);
|
|
276
|
+
await register(client, identity, target !== null);
|
|
238
277
|
return client;
|
|
239
278
|
}
|
|
240
279
|
catch {
|
|
@@ -246,9 +285,10 @@ async function reconnect(paths, identity, stopping) {
|
|
|
246
285
|
export async function runMcpServer(stdin = process.stdin) {
|
|
247
286
|
const paths = getPaths();
|
|
248
287
|
const identity = identityFor(currentHost(undefined, false), process.env, process.cwd());
|
|
288
|
+
const target = pushTarget(process.env, paths.home);
|
|
249
289
|
let client;
|
|
250
290
|
try {
|
|
251
|
-
client = await connect(paths);
|
|
291
|
+
client = await connect(paths, target);
|
|
252
292
|
}
|
|
253
293
|
catch (err) {
|
|
254
294
|
const code = err instanceof MurmurError ? err.code : 'internal';
|
|
@@ -256,7 +296,7 @@ export async function runMcpServer(stdin = process.stdin) {
|
|
|
256
296
|
process.exit(1);
|
|
257
297
|
}
|
|
258
298
|
try {
|
|
259
|
-
await register(client, identity);
|
|
299
|
+
await register(client, identity, target !== null);
|
|
260
300
|
}
|
|
261
301
|
catch (err) {
|
|
262
302
|
const code = err instanceof MurmurError ? err.code : 'internal';
|
|
@@ -294,7 +334,7 @@ export async function runMcpServer(stdin = process.stdin) {
|
|
|
294
334
|
client.onClosed = resolve;
|
|
295
335
|
}),
|
|
296
336
|
]);
|
|
297
|
-
next = stopping ? null : await reconnect(paths, identity, () => stopping);
|
|
337
|
+
next = stopping ? null : await reconnect(paths, identity, target, () => stopping);
|
|
298
338
|
}
|
|
299
339
|
client.close();
|
|
300
340
|
}
|
package/dist/protocol.js
CHANGED
|
@@ -17,6 +17,10 @@ export const HANDLE_RE = /^[a-z0-9][a-z0-9-]{1,47}$/;
|
|
|
17
17
|
export const PROOF_POLL_MS = 120000;
|
|
18
18
|
export const THREADS_DEFAULT = 10;
|
|
19
19
|
export const THREADS_MAX = 50;
|
|
20
|
+
// A notification batch as the plain text a session reads.
|
|
21
|
+
export function noticeText(payloads) {
|
|
22
|
+
return '[murmur notice] ' + payloads.map((p) => p.detail).join('; ');
|
|
23
|
+
}
|
|
20
24
|
export class MurmurError extends Error {
|
|
21
25
|
code;
|
|
22
26
|
constructor(code, message) {
|
package/dist/store.js
CHANGED
|
@@ -85,6 +85,9 @@ CREATE INDEX IF NOT EXISTS idx_sessions_host ON sessions (host_pid, host_start,
|
|
|
85
85
|
const ADDED_COLUMNS = [
|
|
86
86
|
['messages', 'via', 'TEXT'],
|
|
87
87
|
['sessions', 'pinned', 'INTEGER NOT NULL DEFAULT 0'],
|
|
88
|
+
['sessions', 'host_machine', 'TEXT'],
|
|
89
|
+
['sessions', 'host_label', 'TEXT'],
|
|
90
|
+
['sessions', 'host_fingerprint', 'TEXT'],
|
|
88
91
|
];
|
|
89
92
|
// Delivery states that still count as unread for inbox purposes.
|
|
90
93
|
const UNREAD_STATES_SQL = "('queued','sent','delivered')";
|
|
@@ -108,6 +111,9 @@ function toSessionRow(r) {
|
|
|
108
111
|
handle: r.handle,
|
|
109
112
|
hostPid: r.host_pid,
|
|
110
113
|
hostStart: r.host_start,
|
|
114
|
+
hostMachine: r.host_machine,
|
|
115
|
+
hostLabel: r.host_label,
|
|
116
|
+
hostFingerprint: r.host_fingerprint,
|
|
111
117
|
transcriptPath: r.transcript_path,
|
|
112
118
|
cwd: r.cwd,
|
|
113
119
|
state: r.state,
|
|
@@ -447,19 +453,48 @@ export class Store {
|
|
|
447
453
|
.get(client, cliSessionId);
|
|
448
454
|
return rec === undefined ? null : toSessionRow(rec);
|
|
449
455
|
}
|
|
450
|
-
// The newest session recorded for this exact host process
|
|
451
|
-
|
|
456
|
+
// The newest session recorded for this exact host process: pid, start and
|
|
457
|
+
// machine (null is the broker's own machine).
|
|
458
|
+
sessionByHost(hostPid, hostStart, hostMachine) {
|
|
459
|
+
const rec = this.stmt('SELECT * FROM sessions WHERE host_pid = ? AND host_start = ? AND host_machine IS ? ORDER BY updated_at DESC, id DESC LIMIT 1')
|
|
460
|
+
.get(hostPid, hostStart, hostMachine);
|
|
461
|
+
return rec === undefined ? null : toSessionRow(rec);
|
|
462
|
+
}
|
|
463
|
+
// The newest session recorded for this pid and start on any machine.
|
|
464
|
+
sessionOfHost(hostPid, hostStart) {
|
|
452
465
|
const rec = this.stmt('SELECT * FROM sessions WHERE host_pid = ? AND host_start = ? ORDER BY updated_at DESC, id DESC LIMIT 1')
|
|
453
466
|
.get(hostPid, hostStart);
|
|
454
467
|
return rec === undefined ? null : toSessionRow(rec);
|
|
455
468
|
}
|
|
469
|
+
// Every session recorded for this pid and start on any machine, newest first.
|
|
470
|
+
sessionsOfHost(hostPid, hostStart) {
|
|
471
|
+
const recs = this.stmt('SELECT * FROM sessions WHERE host_pid = ? AND host_start = ? ORDER BY updated_at DESC, id DESC').all(hostPid, hostStart);
|
|
472
|
+
return recs.map(toSessionRow);
|
|
473
|
+
}
|
|
474
|
+
// The sessions that have not ended, of the machine with this fingerprint and
|
|
475
|
+
// host name label, recorded under a boot id other than `machine`: a reboot
|
|
476
|
+
// ended them.
|
|
477
|
+
rebootedSessions(fingerprint, label, machine, exceptId) {
|
|
478
|
+
const recs = this.stmt("SELECT * FROM sessions WHERE host_fingerprint = ? AND host_label = ? AND host_machine IS NOT NULL AND host_machine != ? AND id != ? AND (state IS NULL OR state != 'ended')").all(fingerprint, label, machine, exceptId);
|
|
479
|
+
return recs.map(toSessionRow);
|
|
480
|
+
}
|
|
481
|
+
// The sessions recorded under this machine are the broker's own: it learned
|
|
482
|
+
// its boot id after they were written. Their machine, label and fingerprint
|
|
483
|
+
// go back to the own-machine form (NULL), which the process table judges.
|
|
484
|
+
rekeyMachine(machine) {
|
|
485
|
+
this.stmt('UPDATE sessions SET host_machine = NULL, host_label = NULL, host_fingerprint = NULL WHERE host_machine = ?').run(machine);
|
|
486
|
+
}
|
|
456
487
|
// Points one session at another, existing handle and moves its native
|
|
457
488
|
// transport along.
|
|
458
|
-
|
|
489
|
+
// The target's old transport is stale once the session takes the handle; the
|
|
490
|
+
// session's own transport follows unless the old handle keeps another session.
|
|
491
|
+
reassignSession(id, fromHandle, toHandle, moveTransport = true) {
|
|
459
492
|
const move = this.db.transaction(() => {
|
|
460
493
|
this.stmt('UPDATE sessions SET handle = ? WHERE id = ?').run(toHandle, id);
|
|
461
494
|
this.stmt('DELETE FROM native_transports WHERE owner = ?').run(toHandle);
|
|
462
|
-
|
|
495
|
+
if (moveTransport) {
|
|
496
|
+
this.stmt('UPDATE native_transports SET owner = ? WHERE owner = ?').run(toHandle, fromHandle);
|
|
497
|
+
}
|
|
463
498
|
});
|
|
464
499
|
move();
|
|
465
500
|
}
|
|
@@ -492,8 +527,8 @@ export class Store {
|
|
|
492
527
|
return recs.map(toSessionRow);
|
|
493
528
|
}
|
|
494
529
|
insertSession(arg) {
|
|
495
|
-
const info = this.stmt(`INSERT INTO sessions (client, cli_session_id, handle, host_pid, host_start, transcript_path, cwd, state, state_at, started_at, updated_at)
|
|
496
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, NULL, NULL, ?, ?)`).run(arg.client, arg.cliSessionId, arg.handle, arg.hostPid, arg.hostStart, arg.transcriptPath, arg.cwd, arg.now, arg.now);
|
|
530
|
+
const info = this.stmt(`INSERT INTO sessions (client, cli_session_id, handle, host_pid, host_start, host_machine, host_label, host_fingerprint, transcript_path, cwd, state, state_at, started_at, updated_at)
|
|
531
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, ?, ?)`).run(arg.client, arg.cliSessionId, arg.handle, arg.hostPid, arg.hostStart, arg.hostMachine, arg.hostLabel, arg.hostFingerprint, arg.transcriptPath, arg.cwd, arg.now, arg.now);
|
|
497
532
|
return Number(info.lastInsertRowid);
|
|
498
533
|
}
|
|
499
534
|
// Applies only the keys present in the patch.
|
|
@@ -503,6 +538,9 @@ export class Store {
|
|
|
503
538
|
['cliSessionId', 'cli_session_id'],
|
|
504
539
|
['hostPid', 'host_pid'],
|
|
505
540
|
['hostStart', 'host_start'],
|
|
541
|
+
['hostMachine', 'host_machine'],
|
|
542
|
+
['hostLabel', 'host_label'],
|
|
543
|
+
['hostFingerprint', 'host_fingerprint'],
|
|
506
544
|
['transcriptPath', 'transcript_path'],
|
|
507
545
|
['cwd', 'cwd'],
|
|
508
546
|
['state', 'state'],
|