openmsg 0.4.4 → 0.4.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openmsg",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
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
@@ -97,8 +97,19 @@ function ago(ms) {
97
97
  return `${Math.floor(s / 86400)}d`;
98
98
  }
99
99
 
100
+ // Two records name one session when the vendor and the session id agree.
101
+ function sameSession(a, b) {
102
+ return Boolean(a && b && a.vendor === b.vendor && a.id === b.id);
103
+ }
104
+
100
105
  async function cmdList(args = []) {
101
- const all = notes.attach(await allAgents());
106
+ // Several sessions can run in one directory with names that look alike. The
107
+ // mark "(you)" shows the agent which row is its own, so that it does not send
108
+ // to itself. A command outside a session has no row, and it gets no mark.
109
+ const [all, me] = await Promise.all([
110
+ allAgents().then((a) => notes.attach(a)),
111
+ self().catch(() => null),
112
+ ]);
102
113
  const agents = args.includes("--all") ? all : all.filter((a) => !a.background);
103
114
  const hidden = all.length - agents.length;
104
115
  if (agents.length === 0) {
@@ -106,14 +117,15 @@ async function cmdList(args = []) {
106
117
  return;
107
118
  }
108
119
  if (args.includes("--json")) {
109
- console.log(JSON.stringify(agents.map(({ transport, ...a }) => a), null, 1));
120
+ console.log(JSON.stringify(agents.map(({ transport, ...a }) => ({ ...a, you: sameSession(a, me) })), null, 1));
110
121
  return;
111
122
  }
112
123
  const width = Math.max(...agents.map((a) => address(a).length));
113
124
  const tilde = (p) => (p && p.startsWith(os.homedir()) ? `~${p.slice(os.homedir().length)}` : p);
114
125
  for (const a of agents) {
115
126
  const times = [a.startedAt && `started ${ago(a.startedAt)} ago`, a.updatedAt && `active ${ago(a.updatedAt)} ago`];
116
- const head = [address(a).padEnd(width), a.status.padEnd(7), times.filter(Boolean).join(", ")];
127
+ const you = sameSession(a, me) ? "(you)" : "";
128
+ const head = [address(a).padEnd(width), a.status.padEnd(7), times.filter(Boolean).join(", "), you];
117
129
  console.log(head.join(" ").trimEnd());
118
130
  // The note of the agent comes first, because the agent wrote it about the
119
131
  // work of now. The title of the vendor comes next.
@@ -163,6 +175,12 @@ async function cmdSend(target, text, replyTo = null, args = []) {
163
175
  `Its name was ${address(answered.openmsg.from)}. Send a new message instead of a reply.`,
164
176
  );
165
177
  }
178
+ if (sameSession(to, from)) {
179
+ throw new Error(
180
+ `${address(to)} is this session. An agent does not send a message to itself. ` +
181
+ "Leave it out of the recipients.",
182
+ );
183
+ }
166
184
  if (pinned && to.id !== pinned) {
167
185
  throw new Error(
168
186
  `the session that sent ${replyTo} is gone. Its name now belongs to ${address(to)}. ` +
@@ -220,8 +238,9 @@ async function cmdSendProject(target, text, replyTo, args) {
220
238
 
221
239
  // Only the records of Claude Code, because they are files. The discovery of
222
240
  // Codex and OpenCode runs lsof, and that costs seconds.
241
+ // A session that sends to its own directory does not get its own message.
223
242
  const live = claudeAgents()
224
- .filter((a) => projectbox.sessionIsIn(a.cwd, dir))
243
+ .filter((a) => projectbox.sessionIsIn(a.cwd, dir) && !sameSession(a, from))
225
244
  .sort((a, b) => b.updatedAt - a.updatedAt);
226
245
  for (const agent of live) {
227
246
  const got = projectbox.takeMessage(dir, message);
package/src/registry.mjs CHANGED
@@ -19,9 +19,41 @@ function alive(pid) {
19
19
  }
20
20
  }
21
21
 
22
+ // Claude Code keeps its state in ~/.claude, or in the directory that
23
+ // CLAUDE_CONFIG_DIR names. A person can run sessions with more than one such
24
+ // directory, for example ~/.claude-work for a second account. Each session of
25
+ // each directory must be found, so openmsg reads all of them.
26
+ export function claudeConfigDirs() {
27
+ const dirs = [path.join(HOME, ".claude")];
28
+ if (process.env.CLAUDE_CONFIG_DIR) dirs.push(path.resolve(process.env.CLAUDE_CONFIG_DIR));
29
+ try {
30
+ for (const d of fs.readdirSync(HOME)) {
31
+ if (!d.startsWith(".claude-")) continue;
32
+ const full = path.join(HOME, d);
33
+ if (fs.existsSync(path.join(full, "sessions"))) dirs.push(full);
34
+ }
35
+ } catch {
36
+ // The home directory cannot be read.
37
+ }
38
+ return [...new Set(dirs)];
39
+ }
40
+
22
41
  // Claude Code writes one file for each session, with the path of its inbox socket.
23
42
  export function claudeAgents() {
24
- const dir = path.join(HOME, ".claude", "sessions");
43
+ const out = [];
44
+ const seen = new Set();
45
+ for (const config of claudeConfigDirs()) {
46
+ for (const agent of claudeAgentsIn(config)) {
47
+ if (seen.has(agent.id)) continue;
48
+ seen.add(agent.id);
49
+ out.push(agent);
50
+ }
51
+ }
52
+ return out;
53
+ }
54
+
55
+ function claudeAgentsIn(config) {
56
+ const dir = path.join(config, "sessions");
25
57
  let files = [];
26
58
  try {
27
59
  files = fs.readdirSync(dir).filter((f) => f.endsWith(".json"));
@@ -44,7 +76,7 @@ export function claudeAgents() {
44
76
  pid: rec.pid,
45
77
  cwd: rec.cwd,
46
78
  status: rec.status ?? "unknown",
47
- title: claudeTitle(rec.cwd, rec.sessionId),
79
+ title: claudeTitle(config, rec.cwd, rec.sessionId),
48
80
  // A program that uses the SDK, such as a memory observer, starts a
49
81
  // session too. It is not a session that a person talks to.
50
82
  background: rec.entrypoint === "sdk-cli",
@@ -59,8 +91,8 @@ export function claudeAgents() {
59
91
  // Claude Code keeps the transcript of a session in a directory that the
60
92
  // working directory names, with each character that is not a letter or a digit
61
93
  // changed to "-".
62
- function claudeTranscript(cwd, sessionId) {
63
- const root = path.join(HOME, ".claude", "projects");
94
+ function claudeTranscript(config, cwd, sessionId) {
95
+ const root = path.join(config, "projects");
64
96
  const file = `${sessionId}.jsonl`;
65
97
  if (cwd) {
66
98
  const direct = path.join(root, cwd.replace(/[^A-Za-z0-9]/g, "-"), file);
@@ -77,9 +109,9 @@ function claudeTranscript(cwd, sessionId) {
77
109
  return null;
78
110
  }
79
111
 
80
- function claudeTitle(cwd, sessionId) {
112
+ function claudeTitle(config, cwd, sessionId) {
81
113
  if (!sessionId) return null;
82
- const file = claudeTranscript(cwd, sessionId);
114
+ const file = claudeTranscript(config, cwd, sessionId);
83
115
  return file ? lastClaudeTitle(file) : null;
84
116
  }
85
117
 
@@ -436,8 +468,19 @@ export async function self() {
436
468
  return known ?? { vendor: "codex", id, name: id.slice(-8), transport: { kind: "codex-queue" } };
437
469
  }
438
470
  if (process.env.CLAUDE_CODE_MESSAGING_SOCKET) {
439
- const me = claudeAgents().find((a) => a.transport.path === process.env.CLAUDE_CODE_MESSAGING_SOCKET);
471
+ const socket = process.env.CLAUDE_CODE_MESSAGING_SOCKET;
472
+ const me = claudeAgents().find((a) => a.transport.path === socket);
440
473
  if (me && (await runsUnder(me.pid))) return me;
474
+ // A Claude session started this command, but no session record holds its
475
+ // socket. A shell name here is false: nobody can send to it, and an agent
476
+ // gives it to other agents as its own address.
477
+ if (!me) {
478
+ throw new Error(
479
+ `this command runs in a Claude Code session, but openmsg cannot find the record of that session ` +
480
+ `(socket ${socket}). openmsg reads ${claudeConfigDirs().map((d) => `${d}/sessions`).join(", ")}. ` +
481
+ `If the session uses another CLAUDE_CONFIG_DIR, run openmsg from inside it, or set OPENMSG_SELF.`,
482
+ );
483
+ }
441
484
  }
442
485
  const line = await ancestors(process.ppid);
443
486
  let [codex, opencode] = await Promise.all([codexAgents(), opencodeAgents()]);