openmsg 0.5.0 → 0.7.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.
package/README.md CHANGED
@@ -39,6 +39,7 @@ An address is `<vendor>:<name>`, for example `claude:api-worker`.
39
39
  | Cursor CLI | A hook reads the mailbox at the end of each turn | Written, tested with fixtures. A live test needs `CURSOR_API_KEY` |
40
40
  | Gemini CLI | The same hook, as an AfterAgent deny | Written, tested with fixtures. Gemini CLI is not installed here |
41
41
  | An agent that wakes on an HTTP request | `openmsg webhook add <name> <url>`. The request carries a notice with no text of the sender, and the agent reads the message with `openmsg inbox` | Works, tested live with a hosted bot on its own machine, through a relay on a private network |
42
+ | A chat product that takes an MCP connector | `openmsg mcp add <name>`, and `openmsg mcp serve --as <name>` as the server. Nothing wakes the agent: it reads its mailbox with a tool | Works, tested live with two chat products: one through a tunnel of its vendor, and one over HTTP behind a token |
42
43
  | Other agents | `tmux send-keys` | Not started |
43
44
 
44
45
  A reply is a new message. The receiving agent answers with `openmsg send`. No
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openmsg",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Messages between AI coding agents of different vendors, on one machine or between two people, sealed end to end.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.mjs CHANGED
@@ -24,6 +24,8 @@ import * as device from "./device.mjs";
24
24
  import * as dirsync from "./dirsync.mjs";
25
25
  import * as notes from "./notes.mjs";
26
26
  import * as webhooks from "./webhooks.mjs";
27
+ import * as mcpagents from "./mcpagents.mjs";
28
+ import * as mcp from "./mcp.mjs";
27
29
  import { fileURLToPath } from "node:url";
28
30
  import fs from "node:fs";
29
31
  import path from "node:path";
@@ -37,6 +39,7 @@ const USAGE = `openmsg — messages between AI coding agents
37
39
  openmsg send <agent> <text> send a message to an agent
38
40
  [--reply-to <message-id>] keep the conversation of that message
39
41
  openmsg send project:<dir> <text> the next session in that directory takes it
42
+ openmsg send project:<name>@<owner> <text> the same, in a directory of another person
40
43
  [--key <k>] replace the waiting message with that key
41
44
  openmsg clear project:<dir> --key <k> remove the waiting message with that key
42
45
  openmsg waiting the project messages that wait for a session
@@ -46,6 +49,14 @@ const USAGE = `openmsg — messages between AI coding agents
46
49
  [--header "Name: value"] a header that the webhook asks for, such as a key
47
50
  openmsg webhook remove <name> take that agent away
48
51
  openmsg webhook list the agents that wake on an HTTP request
52
+ openmsg mcp add <name> name an agent that reads through MCP, such as a chat product
53
+ [--allow <address>] an address that it can write to. "*" stands for any text
54
+ [--per-minute <n>] how many messages it sends in one minute
55
+ openmsg mcp remove <name> take that agent away
56
+ openmsg mcp list the agents that read through MCP
57
+ openmsg mcp serve --as <name> the MCP server of that agent, on the standard input and output
58
+ [--http --port <n>] the same server over HTTP on the loopback address, behind a token
59
+ [--token-file <file>] the file of that token. openmsg makes it when it is not there
49
60
  openmsg hook <vendor> run inside a hook of claude, cursor, or gemini
50
61
  openmsg install let every agent on this machine answer messages
51
62
  openmsg install --hooks add the hooks for claude, cursor, and gemini
@@ -160,6 +171,10 @@ async function cmdSend(target, text, replyTo = null, args = []) {
160
171
  if (!target || !text) throw new Error('usage: openmsg send <agent> "<text>" [--reply-to <message-id>]');
161
172
  // An alias with an owner, such as "claude:api-worker@alice", belongs to
162
173
  // another person. That message goes through the two gateways.
174
+ // "project:web-app@alice" names a directory of another person by its name.
175
+ // A project address of this machine holds a path, and a path has a slash,
176
+ // a dot, or a tilde.
177
+ if (/^project:[^/.~@][^/@]*@[^/@]+$/.test(target)) return cmdSendRemote(target, text, replyTo, args);
163
178
  if (projectbox.isProjectAddress(target)) return cmdSendProject(target, text, replyTo, args);
164
179
  if (target.includes("@")) return cmdSendRemote(target, text, replyTo, args);
165
180
  const from = await self();
@@ -284,6 +299,17 @@ function cmdWaiting() {
284
299
  for (const { dir, messages } of rows) {
285
300
  for (const m of messages) {
286
301
  const key = m.openmsg.key ? ` [${m.openmsg.key}]` : "";
302
+ if (m.openmsg.version === remote.VERSION) {
303
+ // A message from another person. Its text is for the session that
304
+ // takes it, and the owner reads it with `held`.
305
+ const who = `${m.openmsg.from.vendor}:${m.openmsg.from.name ?? m.openmsg.from.id}@${m.sender?.label ?? m.openmsg.from.owner}`;
306
+ const was = m.placed?.wasFor;
307
+ console.log(`${dir} from ${who} at ${m.createdAt} (${m.messageId.slice(0, 8)})`);
308
+ console.log(was ? ` It was for the session ${was.alias}, which ended.` : " It is for the directory, and not for one session.");
309
+ if (was?.vendor === "claude") console.log(` Open that conversation again: cd ${dir} && claude --resume ${was.session}`);
310
+ console.log(` The next session in that directory takes it. Read it: openmsg held ${m.messageId.slice(0, 8)} --text\n`);
311
+ continue;
312
+ }
287
313
  console.log(`${dir}${key} from ${address(m.openmsg.from)} at ${m.createdAt}\n${textOf(m)}\n`);
288
314
  }
289
315
  }
@@ -342,6 +368,74 @@ async function acknowledgeRead(rows) {
342
368
  }
343
369
  }
344
370
 
371
+ async function cmdMcp(args) {
372
+ // --allow can come more than one time.
373
+ const allow = [];
374
+ const words = [];
375
+ for (let i = 0; i < args.length; i++) {
376
+ if (args[i] === "--allow") {
377
+ if (args[i + 1] === undefined) throw new Error('--allow needs an address, such as "claude:*"');
378
+ allow.push(args[++i]);
379
+ } else if (["--per-minute", "--as", "--port", "--token-file", "--path"].includes(args[i])) {
380
+ i += 1;
381
+ } else if (args[i] !== "--http") words.push(args[i]);
382
+ }
383
+ const [sub, name] = words;
384
+ if (sub === "serve") {
385
+ const as = flag(args, "as");
386
+ if (!as) throw new Error("usage: openmsg mcp serve --as <name> [--http --port <n> --token-file <file>]");
387
+ if (!mcpagents.get(as)) throw new Error(`no agent mcp:${as}. Run: openmsg mcp add ${as}`);
388
+ if (!args.includes("--http")) {
389
+ await mcp.serve({ name: as });
390
+ return;
391
+ }
392
+ // The token is in a file that only the owner reads. No command prints it.
393
+ const file = flag(args, "token-file") ?? path.join(identity.home(), `mcp-${as}.token`);
394
+ if (!fs.existsSync(file)) {
395
+ fs.mkdirSync(path.dirname(file), { recursive: true });
396
+ fs.writeFileSync(file, mcp.newToken(), { mode: 0o600 });
397
+ }
398
+ const token = fs.readFileSync(file, "utf8").trim();
399
+ const stamp = (line) => console.log(`${new Date().toISOString()} ${line}`);
400
+ const running = await mcp.serveHttp({ name: as, token, port: Number(flag(args, "port") ?? 7812), route: flag(args, "path") ?? "/mcp", log: stamp });
401
+ stamp(`mcp:${as} listens on ${running.url}, and each request needs the token in ${file}`);
402
+ stamp('The header of a request: "Authorization: Bearer <token>". A proxy in front gives the address to the network, with TLS.');
403
+ const stop = () => running.close().then(() => process.exit(0));
404
+ process.on("SIGINT", stop);
405
+ process.on("SIGTERM", stop);
406
+ return;
407
+ }
408
+ if (sub === "add") {
409
+ if (!name) throw new Error("usage: openmsg mcp add <name> [--allow <address>] [--per-minute <n>]");
410
+ const per = flag(args, "per-minute");
411
+ const row = mcpagents.add(name, { allow, perMinute: per === null ? null : Number(per) });
412
+ console.log(`mcp:${row.name} reads its mailbox through MCP, and nothing wakes it`);
413
+ console.log(
414
+ row.allow.length > 0
415
+ ? `It can write to: ${row.allow.join(", ")}`
416
+ : `It can write to nobody yet. Give an address: openmsg mcp add ${row.name} --allow "<address>"`,
417
+ );
418
+ console.log(`It sends ${row.perMinute} messages in one minute at most.`);
419
+ console.log(`The command of its connector: openmsg mcp serve --as ${row.name}`);
420
+ return;
421
+ }
422
+ if (sub === "remove") {
423
+ if (!name) throw new Error("usage: openmsg mcp remove <name>");
424
+ console.log(mcpagents.remove(name) ? `mcp:${name} is gone` : `no agent mcp:${name}`);
425
+ return;
426
+ }
427
+ if (sub === undefined || sub === "list") {
428
+ const rows = mcpagents.agents();
429
+ if (rows.length === 0) console.log("no agent reads through MCP. Run: openmsg mcp add <name>");
430
+ for (const a of rows) {
431
+ const row = mcpagents.get(a.name);
432
+ console.log(`${address(a)} writes to: ${row.allow.join(", ") || "nobody"} · ${row.perMinute} in one minute`);
433
+ }
434
+ return;
435
+ }
436
+ throw new Error("usage: openmsg mcp add <name> | mcp remove <name> | mcp list | mcp serve --as <name>");
437
+ }
438
+
345
439
  function cmdWebhook(args) {
346
440
  // --header can come more than one time, and each one takes the next word.
347
441
  const headers = [];
@@ -426,11 +520,24 @@ async function cmdSendRemote(target, text, replyTo, args) {
426
520
  );
427
521
  }
428
522
  const receipts = [];
429
- const link = direct ? null : await relaylink.open(relayUrl, { onReceipt: (frame) => receipts.push(frame) });
523
+ // A relay that does not answer is not the end of a message. The machine of
524
+ // the relay can sleep, and the message then waits in the outbox of this
525
+ // machine, with the routing data that this machine read before.
526
+ let link = null;
527
+ let unreachable = null;
528
+ if (!direct) {
529
+ try {
530
+ link = await relaylink.open(relayUrl, { onReceipt: (frame) => receipts.push(frame) });
531
+ } catch (e) {
532
+ unreachable = e.message;
533
+ }
534
+ }
430
535
 
431
536
  let rows;
432
537
  let label = member.label;
538
+ let fromCache = false;
433
539
  try {
540
+ if (unreachable) throw new Error(unreachable);
434
541
  const answer = direct
435
542
  ? await gateway.routingOf(member, project)
436
543
  : await gateway.routingOverRelay(link, member, project);
@@ -448,6 +555,7 @@ async function cmdSendRemote(target, text, replyTo, args) {
448
555
  console.error(`openmsg: ${e.message}`);
449
556
  console.error(`openmsg: using the routing data of ${cached.at}`);
450
557
  rows = cached.rows;
558
+ fromCache = true;
451
559
  }
452
560
 
453
561
  const row = rows.find((r) => r.alias === alias || r.name === alias || r.session === alias);
@@ -456,7 +564,9 @@ async function cmdSendRemote(target, text, replyTo, args) {
456
564
  const names = rows.map((r) => `${r.alias}@${label}`).join(", ") || "nothing";
457
565
  throw new Error(`${member.label} does not publish "${alias}" in this project. Published: ${names}`);
458
566
  }
459
- if (row.live === false) console.error(`openmsg: the session ${row.alias}@${label} does not run now`);
567
+ if (row.live === false && row.vendor !== "project") {
568
+ console.error(`openmsg: the session ${row.alias}@${label} does not run now. The next session in its directory takes the message.`);
569
+ }
460
570
  // The message is sealed for the machine that holds that session, and the
461
571
  // routing data of that machine gave this key.
462
572
  const sealTo = row.sealTo ?? null;
@@ -472,30 +582,49 @@ async function cmdSendRemote(target, text, replyTo, args) {
472
582
  replyTo: answered?.messageId ?? null,
473
583
  hops: answered?.message?.openmsg?.hops ?? [],
474
584
  workspace: remote.workspaceOf(),
585
+ // The sender knows here that the message will wait.
586
+ ttlMs: fromCache || row.live === false ? remote.WAIT_TTL_MS : remote.DEFAULT_TTL_MS,
475
587
  });
476
588
  const wire = remote.pack(message, { recipient: member, sealTo });
477
589
  const how = {
478
590
  "adapter-accepted": `reached the agent of ${member.label}`,
479
591
  held: `waits for ${member.label} to accept you`,
480
- queued: `waits in the mailbox of ${member.label}`,
592
+ queued: `waits on the machine of ${member.label}, for an agent that reads its mailbox or for the next session in that directory`,
481
593
  "relay-holds": `the relay holds it for ${member.label}, who is offline now`,
594
+ "outbox-holds":
595
+ `waits on this machine for ${member.label}, because the relay does not answer. ` +
596
+ "Your gateway sends it when the relay answers. See it: openmsg outbox",
482
597
  };
598
+ const row0 = { messageId: message.messageId, to: member.ownerId, project, alias: `${alias}@${label}`, wire };
483
599
  let out;
484
600
  if (direct) {
485
601
  out = await gateway.send(member, wire);
602
+ } else if (!link) {
603
+ outbox.put({ ...row0, state: "queued", pending: true });
604
+ out = { status: "outbox-holds" };
486
605
  } else {
487
606
  // The sender keeps its copy until a receipt arrives. The relay keeps its
488
607
  // own copy at the same time.
489
- outbox.put({ messageId: message.messageId, to: member.ownerId, project, alias: `${alias}@${label}`, wire, state: "sending" });
608
+ outbox.put({ ...row0, state: "sending" });
490
609
  const answer = await link.send(wire);
491
- if (answer.type !== "stored") {
610
+ if (answer.type !== "stored" && gateway.RELAY_GONE.includes(answer.reason)) {
611
+ // The connection stopped before the relay said that it holds the message.
612
+ outbox.setState(message.messageId, "queued", { pending: true });
613
+ link.close();
614
+ link = null;
615
+ out = { status: "outbox-holds" };
616
+ } else if (answer.type !== "stored") {
492
617
  outbox.setState(message.messageId, "refused", { reason: answer.reason });
493
618
  link.close();
494
619
  console.log(`refused by the relay: ${answer.reason} (${message.messageId})`);
495
620
  process.exitCode = 1;
496
621
  return;
497
622
  }
623
+ }
624
+ if (link) {
498
625
  outbox.setState(message.messageId, "queued");
626
+ // A message that waited here from an earlier try goes on the same connection.
627
+ await gateway.flushOutbox(link).catch(() => {});
499
628
  // A receipt says what happened at the other end. It can arrive in a
500
629
  // moment, or days later, and then the gateway of this owner takes it.
501
630
  const receipt = await waitForReceipt(receipts, message.messageId, 3000);
@@ -675,7 +804,8 @@ function cmdOutbox(args) {
675
804
  return;
676
805
  }
677
806
  for (const r of rows) {
678
- console.log(`${r.messageId.slice(0, 8)} ${String(r.state).padEnd(16)} to ${r.alias ?? r.to} ${r.at}`);
807
+ const where = r.pending ? " (on this machine, the relay did not answer)" : "";
808
+ console.log(`${r.messageId.slice(0, 8)} ${String(r.state).padEnd(16)} to ${r.alias ?? r.to} ${r.at}${where}`);
679
809
  }
680
810
  }
681
811
 
@@ -1084,6 +1214,7 @@ try {
1084
1214
  else if (cmd === "invite") cmdInvite(args);
1085
1215
  else if (cmd === "dir") await cmdDir(args);
1086
1216
  else if (cmd === "webhook") cmdWebhook(args);
1217
+ else if (cmd === "mcp") await cmdMcp(args);
1087
1218
  else if (cmd === "whoami") console.log(address(await self()));
1088
1219
  else console.log(USAGE);
1089
1220
  } catch (e) {
package/src/deliver.mjs CHANGED
@@ -31,9 +31,19 @@ export async function deliverLocal(agent, message, { text = null, parts = null }
31
31
  if (agent.vendor === "opencode") return opencode.deliver(agent, message, parts ? { layout: parts } : { text });
32
32
  if (agent.vendor === "codex") return codex.deliver(agent, message, { text: plain, highlight: parts ? bodyRange(parts) : null });
33
33
  if (agent.vendor === "webhook") return wake(agent, message, plain);
34
- // An agent with no push entry point, such as Gemini CLI or Cursor CLI, reads
35
- // its mailbox at the end of a turn. The message waits there.
36
- return { delivered: false, transport: "mailbox" };
34
+ // An agent with no push entry point reads its mailbox: Gemini CLI and Cursor
35
+ // CLI at the end of a turn, and an MCP agent when its user asks. The
36
+ // message waits there, with the text that the model reads. A message from
37
+ // another person has no other way into that mailbox.
38
+ keep(agent, message, plain);
39
+ return { delivered: false, transport: "mailbox", stored: true };
40
+ }
41
+
42
+ // A message that arrives a second time, before the agent read the first copy,
43
+ // does not go to the mailbox twice.
44
+ function keep(agent, message, rendered) {
45
+ const waits = mailbox.list(agent, { unreadOnly: true }).some((m) => m.messageId === message.messageId);
46
+ if (!waits) mailbox.put(agent, { ...message, rendered }, "queued");
37
47
  }
38
48
 
39
49
  // An agent that wakes on an HTTP request. The message goes to the mailbox
@@ -41,10 +51,8 @@ export async function deliverLocal(agent, message, { text = null, parts = null }
41
51
  // The agent that wakes therefore always finds the message. `stored` tells the
42
52
  // caller that the mailbox holds the message already.
43
53
  async function wake(agent, message, rendered) {
44
- // A message that arrives a second time, before the agent read the first
45
- // copy, does not go to the mailbox twice. The agent still gets a new notice.
46
- const waits = mailbox.list(agent, { unreadOnly: true }).some((m) => m.messageId === message.messageId);
47
- if (!waits) mailbox.put(agent, { ...message, rendered }, "queued");
54
+ // The agent gets a new notice for a message that arrives a second time.
55
+ keep(agent, message, rendered);
48
56
  try {
49
57
  return { ...(await webhook.deliver(agent, message)), stored: true };
50
58
  } catch (e) {
package/src/gateway.mjs CHANGED
@@ -22,6 +22,8 @@ import * as identity from "./identity.mjs";
22
22
  import * as directory from "./directory.mjs";
23
23
  import * as permissions from "./permissions.mjs";
24
24
  import * as published from "./published.mjs";
25
+ import * as places from "./places.mjs";
26
+ import * as projectbox from "./projectbox.mjs";
25
27
  import * as inbound from "./inbound.mjs";
26
28
  import * as remote from "./remote.mjs";
27
29
  import { agentsOfVendor } from "./registry.mjs";
@@ -81,6 +83,9 @@ async function liveAgents(vendor, { maxAgeMs = 5000 } = {}) {
81
83
  if (held && Date.now() - held.at < maxAgeMs) return held.rows;
82
84
  const rows = await agentsOfVendor(vendor);
83
85
  agentCache.set(vendor, { at: Date.now(), rows });
86
+ // The directory of each session stays on record, for the day that the
87
+ // session ends and a message for it arrives.
88
+ places.remember(rows);
84
89
  return rows;
85
90
  }
86
91
 
@@ -110,13 +115,13 @@ function openRow(projectId, agent) {
110
115
  // Every row of a project: the sessions that the owner named, and, in an open
111
116
  // project, each other session that runs now. A background session, such as a
112
117
  // program that uses the SDK, is not a session of a person, and it stays out.
113
- async function rowsOf(projectId) {
118
+ async function rowsOf(projectId, { maxAgeMs = 5000 } = {}) {
114
119
  const rows = published.list(projectId);
115
120
  if (!published.isOpen(projectId)) return rows;
116
121
  const sessions = new Set(rows.map((r) => r.session));
117
122
  const aliases = new Set(rows.map((r) => r.alias));
118
123
  for (const vendor of openVendors()) {
119
- for (const agent of await liveAgents(vendor)) {
124
+ for (const agent of await liveAgents(vendor, { maxAgeMs })) {
120
125
  if (agent.vendor !== vendor || agent.background) continue;
121
126
  // A name that the owner published holds its session and its epoch.
122
127
  if (sessions.has(agent.id) || aliases.has(published.aliasOf(agent))) continue;
@@ -140,6 +145,37 @@ async function rowOf(projectId, session, vendorHint = null) {
140
145
  return null;
141
146
  }
142
147
 
148
+ // The places of a project: name -> directory. A place is the directory of a
149
+ // session that the project reaches. In an open project that is each session
150
+ // of an allowed vendor. In another project it is each session that the owner
151
+ // published by name.
152
+ function reached(projectId) {
153
+ const named = new Set(published.list(projectId).map((r) => r.session));
154
+ const open = published.isOpen(projectId);
155
+ const vendors = openVendors();
156
+ return (row) => named.has(row.id) || (open && vendors.includes(row.vendor));
157
+ }
158
+
159
+ function placesOf(projectId) {
160
+ return places.dirs({ allow: reached(projectId) });
161
+ }
162
+
163
+ // The sessions of a directory that the project reaches and that run now, the
164
+ // most recent first.
165
+ async function sessionsIn(projectId, dir, { except = null, maxAgeMs = 5000 } = {}) {
166
+ const rows = await rowsOf(projectId, { maxAgeMs });
167
+ const out = [];
168
+ for (const vendor of new Set(rows.map((r) => r.vendor))) {
169
+ // rowsOf asked each vendor a moment ago, so this read costs nothing.
170
+ for (const agent of await liveAgents(vendor)) {
171
+ if (agent.id === except || agent.background) continue;
172
+ if (!rows.some((r) => r.session === agent.id)) continue;
173
+ if (projectbox.sessionIsIn(agent.cwd, dir)) out.push(agent);
174
+ }
175
+ }
176
+ return out.sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0));
177
+ }
178
+
143
179
  // The two descriptions of section 9.4 of the spec. Presence is for a person,
144
180
  // and routing data is for a gateway. Neither one carries a working directory,
145
181
  // a socket path, or a token.
@@ -164,6 +200,33 @@ export async function descriptions(projectId) {
164
200
  live: Boolean(agent),
165
201
  });
166
202
  }
203
+ // A session that ended stays in the answer while this machine knows its
204
+ // directory. A sender that still holds its name can write to it, and the
205
+ // message goes to that directory.
206
+ const aliases = new Set(routing.map((r) => r.alias));
207
+ const sessions = new Set(routing.map((r) => r.session));
208
+ for (const seen of places.sessions({ allow: reached(projectId) })) {
209
+ const alias = published.aliasOf(seen);
210
+ if (sessions.has(seen.id) || aliases.has(alias) || !fs.existsSync(seen.dir)) continue;
211
+ aliases.add(alias);
212
+ presence.push({ alias: `${alias}@${me.label}`, vendor: seen.vendor, status: "ended" });
213
+ routing.push({ alias, vendor: seen.vendor, name: seen.name, owner: me.ownerId, project: projectId, session: seen.id, epoch: 1, live: false });
214
+ }
215
+ // A place is a row too. Its session id names the place, and no session.
216
+ for (const [name, dir] of placesOf(projectId)) {
217
+ const live = (await sessionsIn(projectId, dir)).length > 0;
218
+ presence.push({ alias: `project:${name}@${me.label}`, vendor: "project", status: live ? "live" : "waits" });
219
+ routing.push({
220
+ alias: `project:${name}`,
221
+ vendor: "project",
222
+ name,
223
+ owner: me.ownerId,
224
+ project: projectId,
225
+ session: `${places.PLACE}${name}`,
226
+ epoch: 1,
227
+ live,
228
+ });
229
+ }
167
230
  return { presence, routing };
168
231
  }
169
232
 
@@ -305,13 +368,31 @@ export async function release(record, { deliver = deliverLocal, now = Date.now()
305
368
  }
306
369
  if (!(Date.parse(record.expiresAt) > now)) return stop("expired", `the deadline passed at ${record.expiresAt}`);
307
370
 
371
+ // A target can name a directory. A session there takes the message.
372
+ if (places.isPlace(record.target.session)) {
373
+ const name = places.nameOfPlace(record.target.session);
374
+ const dir = placesOf(record.project).get(name);
375
+ if (!dir) return stop("not-published", `no directory with the name ${name} is published in this project`);
376
+ return releaseToPlace(record, { dir, deliver, log, stop });
377
+ }
378
+
308
379
  const mine = await rowOf(record.project, record.target.session, record.message?.openmsg?.to?.vendor ?? null);
309
- if (!mine) return stop("not-published", "that session is not published in this project");
380
+ if (!mine) {
381
+ // A session of an open project that ended has no row. Its directory is
382
+ // on record, and the message goes there.
383
+ const ended = endedSession(record);
384
+ if (ended) return releaseToPlace(record, { dir: ended.dir, wasFor: ended, deliver, log, stop });
385
+ return stop("not-published", "that session is not published in this project");
386
+ }
310
387
  if (record.target.epoch !== mine.epoch) {
311
388
  return stop("old-epoch", `the message names epoch ${record.target.epoch}, and the session runs epoch ${mine.epoch}`);
312
389
  }
313
390
  const agent = (await liveAgents(mine.vendor, { maxAgeMs: 0 })).find((a) => a.id === record.target.session);
314
- if (!agent) return stop("session-gone", "that session does not run now");
391
+ if (!agent) {
392
+ const ended = endedSession(record, mine);
393
+ if (ended) return releaseToPlace(record, { dir: ended.dir, wasFor: ended, deliver, log, stop });
394
+ return stop("session-gone", "that session does not run now");
395
+ }
315
396
 
316
397
  let result;
317
398
  try {
@@ -327,6 +408,60 @@ export async function release(record, { deliver = deliverLocal, now = Date.now()
327
408
  return { status, messageId: record.messageId, transport: result.transport };
328
409
  }
329
410
 
411
+ // The session that a message names, when that session ended and this machine
412
+ // still knows its directory. `row` is the published row of that session. With
413
+ // no row, the project must be open, and the vendor must be one that an open
414
+ // project reaches.
415
+ function endedSession(record, row = null) {
416
+ const seen = places.ofSession(record.target.session);
417
+ if (!seen || !fs.existsSync(seen.dir)) return null;
418
+ if (!row && !(published.isOpen(record.project) && openVendors().includes(seen.vendor))) return null;
419
+ return { alias: row?.alias ?? published.aliasOf(seen), vendor: seen.vendor, session: seen.id, dir: seen.dir };
420
+ }
421
+
422
+ // The message goes to a directory. A session that runs there takes it now.
423
+ // With no session there, it waits in the box of the directory, and the hook
424
+ // of the next session there gives it to that session.
425
+ async function releaseToPlace(record, { dir, wasFor = null, deliver, log, stop }) {
426
+ const placed = {
427
+ place: places.nameOf(dir),
428
+ wasFor: wasFor ? { alias: wasFor.alias, vendor: wasFor.vendor, session: wasFor.session } : null,
429
+ };
430
+ const view = { ...record, placed };
431
+ const text = remote.render(view);
432
+ // A session that ended a moment ago must not take the message, so the
433
+ // gateway asks each vendor again here.
434
+ const [agent] = await sessionsIn(record.project, dir, { except: wasFor?.session ?? null, maxAgeMs: 0 });
435
+ if (!agent) {
436
+ projectbox.put(dir, { ...record.message, rendered: text, placed: { ...placed, dir }, sender: record.from });
437
+ inbound.put({ ...view, status: "queued", reason: "waits-for-a-session", transport: "project-box" });
438
+ log(`queued ${record.messageId} from ${record.from.label} in ${dir}: no session runs there`);
439
+ return { status: "queued", reason: "waits-for-a-session", messageId: record.messageId, transport: "project-box" };
440
+ }
441
+ let result;
442
+ try {
443
+ result = await deliver(agent, record.message, { text, parts: remote.layoutOf(view) });
444
+ } catch (e) {
445
+ return stop("adapter-failed", e.message);
446
+ }
447
+ const status = result.delivered ? "adapter-accepted" : "queued";
448
+ inbound.put({ ...view, status, transport: result.transport, deliveredTo: agent.id });
449
+ log(`${status} ${record.messageId} from ${record.from.label} to ${agent.vendor}:${agent.name}, in ${placed.place}`);
450
+ return { status, messageId: record.messageId, transport: result.transport };
451
+ }
452
+
453
+ // A hook gave a message of the box to a session, and it wrote that state with
454
+ // no word to the sender, because a hook must be fast. The gateway tells the
455
+ // sender here.
456
+ export async function reportPending({ log = () => {} } = {}) {
457
+ for (const row of inbound.list().filter((r) => r.unreported)) {
458
+ const told = await reportState(row, row.status).catch((e) => ({ sent: false, why: e.message }));
459
+ if (!told.sent) continue;
460
+ inbound.setStatus(row.messageId, row.status, { unreported: false });
461
+ log(`${row.from.label} knows that ${row.messageId} is ${row.status}`);
462
+ }
463
+ }
464
+
330
465
  function readBody(req) {
331
466
  return new Promise((resolve, reject) => {
332
467
  let size = 0;
@@ -501,8 +636,21 @@ export async function routingOverRelay(link, member, projectId) {
501
636
  if (!first) {
502
637
  throw new Error(`${member.label ?? member.ownerId}: ${faults[0] ?? "no machine of that person answered"}`);
503
638
  }
504
- directory.setRouting(projectId, member.ownerId, rows);
505
- return { ...first, routing: rows };
639
+ const unique = uniqueRows(rows);
640
+ directory.setRouting(projectId, member.ownerId, unique);
641
+ return { ...first, routing: unique };
642
+ }
643
+
644
+ // One machine can hold two connections to the relay, and then it answers two
645
+ // times. A row is one alias, one session, and one machine key.
646
+ export function uniqueRows(rows) {
647
+ const seen = new Set();
648
+ return rows.filter((row) => {
649
+ const key = JSON.stringify([row.alias, row.session, row.epoch, row.sealTo ?? null]);
650
+ if (seen.has(key)) return false;
651
+ seen.add(key);
652
+ return true;
653
+ });
506
654
  }
507
655
 
508
656
  // The directory of section 12.1: this owner publishes its own records, and it
@@ -537,10 +685,51 @@ export async function syncDirectory(link, { log = () => {}, projects = [] } = {}
537
685
  return out;
538
686
  }
539
687
 
688
+ // The two answers of a connection that stopped. They say nothing about the
689
+ // message, so the message stays with the sender.
690
+ export const RELAY_GONE = ["the relay closed the connection", "the relay did not answer"];
691
+
692
+ // The messages that wait in the outbox of this machine, because the relay did
693
+ // not answer when the agent sent them. Each one goes to the relay now. A
694
+ // message that passed its deadline does not go.
695
+ let flushing = false;
696
+ export async function flushOutbox(link, { log = () => {}, now = Date.now() } = {}) {
697
+ if (flushing) return { sent: 0 };
698
+ flushing = true;
699
+ let sent = 0;
700
+ try {
701
+ for (const row of outbox.list().filter((r) => r.pending && r.wire)) {
702
+ if (!(Date.parse(row.wire.expiresAt) > now)) {
703
+ outbox.setState(row.messageId, "expired", { pending: false, reason: "expired" });
704
+ log(`${row.messageId} passed its deadline in the outbox`);
705
+ continue;
706
+ }
707
+ const answer = await link.send(row.wire);
708
+ if (answer.type === "stored") {
709
+ outbox.setState(row.messageId, "queued", { pending: false });
710
+ sent += 1;
711
+ log(`sent ${row.messageId} to ${row.alias ?? row.to} from the outbox`);
712
+ } else if (RELAY_GONE.includes(answer.reason)) {
713
+ break;
714
+ } else {
715
+ outbox.setState(row.messageId, "refused", { pending: false, reason: answer.reason });
716
+ log(`the relay refused ${row.messageId} from the outbox: ${answer.reason}`);
717
+ }
718
+ }
719
+ } finally {
720
+ flushing = false;
721
+ }
722
+ return { sent };
723
+ }
724
+
725
+ // How often a gateway looks at its outbox while the relay answers. An agent
726
+ // can fail to reach the relay while the connection of the gateway holds.
727
+ export const FLUSH_MS = 30_000;
728
+
540
729
  // The gateway holds one connection to the relay of its team. The relay pushes
541
730
  // a message into it, and the gateway answers with the state that the message
542
731
  // reached here.
543
- export async function joinRelay(url, { log = () => {}, deliver = deliverLocal } = {}) {
732
+ export async function joinRelay(url, { log = () => {}, deliver = deliverLocal, flushMs = FLUSH_MS } = {}) {
544
733
  // Every handler takes the link from its own call. The relay pushes the
545
734
  // waiting messages the moment the connection opens, before the caller of
546
735
  // keep() holds anything.
@@ -549,6 +738,7 @@ export async function joinRelay(url, { log = () => {}, deliver = deliverLocal }
549
738
  log(`relay ${url}: connected as ${link.owner}, ${link.welcome?.waiting ?? 0} waiting`);
550
739
  // The directory travels on the same connection, at each start.
551
740
  syncDirectory(link, { log }).catch((e) => log(`the directory did not travel: ${e.message}`));
741
+ flushOutbox(link, { log }).catch((e) => log(`the outbox did not go: ${e.message}`));
552
742
  },
553
743
  onClose: () => log(`relay ${url}: the connection closed`),
554
744
  onError: (e) => log(`relay ${url}: ${e.error}`),
@@ -581,7 +771,18 @@ export async function joinRelay(url, { log = () => {}, deliver = deliverLocal }
581
771
  );
582
772
  },
583
773
  };
584
- return relaylink.keep(url, handlers);
774
+ const holder = await relaylink.keep(url, handlers);
775
+ const timer = setInterval(() => {
776
+ if (!holder.link || holder.link.closed) return;
777
+ flushOutbox(holder.link, { log }).catch(() => {});
778
+ reportPending({ log }).catch(() => {});
779
+ }, flushMs);
780
+ const stop = holder.stop.bind(holder);
781
+ holder.stop = () => {
782
+ clearInterval(timer);
783
+ stop();
784
+ };
785
+ return holder;
585
786
  }
586
787
 
587
788
  export async function send(member, wire, { timeoutMs = 30_000 } = {}) {