openmsg 0.4.2 → 0.4.4
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 +2 -1
- package/package.json +1 -1
- package/src/adapters/opencode.mjs +20 -3
- package/src/cli.mjs +49 -8
- package/src/deliver.mjs +3 -2
- package/src/install.mjs +2 -1
- package/src/notes.mjs +50 -0
- package/src/registry.mjs +168 -22
- package/src/selfregistry.mjs +1 -0
package/README.md
CHANGED
|
@@ -22,7 +22,8 @@ Each vendor has its own way to accept a message while it runs. openmsg uses that
|
|
|
22
22
|
native way, and gives all of them one command:
|
|
23
23
|
|
|
24
24
|
```
|
|
25
|
-
openmsg list # the agents that run now,
|
|
25
|
+
openmsg list # the agents that run now, and the work of each
|
|
26
|
+
openmsg note "<text>" # tell the other agents what this one does
|
|
26
27
|
openmsg send <agent> "<text>" # deliver a message now
|
|
27
28
|
openmsg inbox # the messages for this agent
|
|
28
29
|
openmsg whoami # how other agents address this one
|
package/package.json
CHANGED
|
@@ -2,14 +2,31 @@
|
|
|
2
2
|
// The OpenCode user interface is a client of a local HTTP server. Any program
|
|
3
3
|
// can send a prompt to a session with one request.
|
|
4
4
|
// Source: https://opencode.ai/docs/server/
|
|
5
|
-
|
|
5
|
+
//
|
|
6
|
+
// OpenCode draws the text of a user message in one color. It shows no escape
|
|
7
|
+
// sequence and no markdown there. It shows only the first text part that is not
|
|
8
|
+
// `synthetic`, and the model reads every part, in order. So the header, the text
|
|
9
|
+
// of the sender, and the line of dashes after it go in one part that the person
|
|
10
|
+
// sees. The notice and the answer command go in a synthetic part: the model
|
|
11
|
+
// reads them, and the screen does not show them.
|
|
12
|
+
// Source: packages/opencode/src/cli/cmd/tui/routes/session/index.tsx (v1.1.11),
|
|
13
|
+
// packages/opencode/src/session/message-v2.ts.
|
|
14
|
+
import { render, RULE } from "../envelope.mjs";
|
|
6
15
|
|
|
7
|
-
export
|
|
16
|
+
export function partsOf({ head, body, foot }) {
|
|
17
|
+
const cut = foot.indexOf(RULE) + 1;
|
|
18
|
+
const shown = { type: "text", text: [...head, body, ...foot.slice(0, cut)].join("\n") };
|
|
19
|
+
const rest = foot.slice(cut);
|
|
20
|
+
return rest.length ? [shown, { type: "text", synthetic: true, text: rest.join("\n") }] : [shown];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function deliver(agent, message, { text = null, layout = null } = {}) {
|
|
8
24
|
const url = `${agent.transport.base}/session/${agent.id}/prompt_async`;
|
|
25
|
+
const parts = layout ? partsOf(layout) : [{ type: "text", text: text ?? render(message) }];
|
|
9
26
|
const res = await fetch(url, {
|
|
10
27
|
method: "POST",
|
|
11
28
|
headers: { "content-type": "application/json" },
|
|
12
|
-
body: JSON.stringify({ parts
|
|
29
|
+
body: JSON.stringify({ parts }),
|
|
13
30
|
signal: AbortSignal.timeout(5000),
|
|
14
31
|
});
|
|
15
32
|
if (!res.ok) throw new Error(`opencode ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
package/src/cli.mjs
CHANGED
|
@@ -22,13 +22,17 @@ import * as outbox from "./outbox.mjs";
|
|
|
22
22
|
import * as limits from "./limits.mjs";
|
|
23
23
|
import * as device from "./device.mjs";
|
|
24
24
|
import * as dirsync from "./dirsync.mjs";
|
|
25
|
+
import * as notes from "./notes.mjs";
|
|
25
26
|
import { fileURLToPath } from "node:url";
|
|
26
27
|
import fs from "node:fs";
|
|
27
28
|
import path from "node:path";
|
|
29
|
+
import os from "node:os";
|
|
28
30
|
|
|
29
31
|
const USAGE = `openmsg — messages between AI coding agents
|
|
30
32
|
|
|
31
|
-
openmsg list
|
|
33
|
+
openmsg list [--all] [--json] show the agents that run now, and their work
|
|
34
|
+
openmsg note "<text>" tell the other agents what this session does
|
|
35
|
+
[--clear] remove the note
|
|
32
36
|
openmsg send <agent> <text> send a message to an agent
|
|
33
37
|
[--reply-to <message-id>] keep the conversation of that message
|
|
34
38
|
openmsg send project:<dir> <text> the next session in that directory takes it
|
|
@@ -83,18 +87,54 @@ A message to another person goes to "claude:api-worker@alice".
|
|
|
83
87
|
Addresses look like "claude:api-worker" or "opencode:ses_123".
|
|
84
88
|
`;
|
|
85
89
|
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
// "5m", "3h", "2d": the time since a moment, in the largest whole unit.
|
|
91
|
+
function ago(ms) {
|
|
92
|
+
if (!ms) return null;
|
|
93
|
+
const s = Math.max(0, Math.round((Date.now() - ms) / 1000));
|
|
94
|
+
if (s < 60) return `${s}s`;
|
|
95
|
+
if (s < 3600) return `${Math.floor(s / 60)}m`;
|
|
96
|
+
if (s < 86400) return `${Math.floor(s / 3600)}h`;
|
|
97
|
+
return `${Math.floor(s / 86400)}d`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function cmdList(args = []) {
|
|
101
|
+
const all = notes.attach(await allAgents());
|
|
102
|
+
const agents = args.includes("--all") ? all : all.filter((a) => !a.background);
|
|
103
|
+
const hidden = all.length - agents.length;
|
|
88
104
|
if (agents.length === 0) {
|
|
89
|
-
console.log("no running agents found");
|
|
105
|
+
console.log(hidden ? `no running agents found. ${hidden} background sessions: openmsg list --all` : "no running agents found");
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (args.includes("--json")) {
|
|
109
|
+
console.log(JSON.stringify(agents.map(({ transport, ...a }) => a), null, 1));
|
|
90
110
|
return;
|
|
91
111
|
}
|
|
92
112
|
const width = Math.max(...agents.map((a) => address(a).length));
|
|
113
|
+
const tilde = (p) => (p && p.startsWith(os.homedir()) ? `~${p.slice(os.homedir().length)}` : p);
|
|
93
114
|
for (const a of agents) {
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
console.log(
|
|
115
|
+
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(", ")];
|
|
117
|
+
console.log(head.join(" ").trimEnd());
|
|
118
|
+
// The note of the agent comes first, because the agent wrote it about the
|
|
119
|
+
// work of now. The title of the vendor comes next.
|
|
120
|
+
const about = a.note ? `note: ${a.note}` : a.title ?? "(no title)";
|
|
121
|
+
console.log(` ${[about, tilde(a.cwd)].filter(Boolean).join(" · ")}`);
|
|
122
|
+
}
|
|
123
|
+
if (hidden) console.log(`\n${hidden} background sessions not shown. See them: openmsg list --all`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function cmdNote(args) {
|
|
127
|
+
const me = await self();
|
|
128
|
+
if (me.vendor === "shell" || me.unresolved) {
|
|
129
|
+
throw new Error("only an agent session has a note. Run this command from inside the agent.");
|
|
130
|
+
}
|
|
131
|
+
const text = args.includes("--clear") ? "" : args.filter((a) => a !== "--clear").join(" ");
|
|
132
|
+
if (!text && !args.includes("--clear")) {
|
|
133
|
+
console.log(notes.get(me) ?? "no note. Write one: openmsg note \"<what you work on>\"");
|
|
134
|
+
return;
|
|
97
135
|
}
|
|
136
|
+
const note = notes.set(me, text);
|
|
137
|
+
console.log(note ? `${address(me)}: ${note}` : `${address(me)}: note removed`);
|
|
98
138
|
}
|
|
99
139
|
|
|
100
140
|
async function cmdSend(target, text, replyTo = null, args = []) {
|
|
@@ -897,7 +937,8 @@ function commandName() {
|
|
|
897
937
|
|
|
898
938
|
const [cmd, ...args] = process.argv.slice(2);
|
|
899
939
|
try {
|
|
900
|
-
if (cmd === "list") await cmdList();
|
|
940
|
+
if (cmd === "list") await cmdList(args);
|
|
941
|
+
else if (cmd === "note") await cmdNote(args);
|
|
901
942
|
else if (cmd === "send") {
|
|
902
943
|
const rest = positional(args, ["reply-to", "project", "key"]);
|
|
903
944
|
await cmdSend(rest[0], rest.slice(1).join(" "), flag(args, "reply-to"), args);
|
package/src/deliver.mjs
CHANGED
|
@@ -12,7 +12,8 @@ import { layoutOf, compose, bodyRange } from "./envelope.mjs";
|
|
|
12
12
|
// and the text of the sender is blue.
|
|
13
13
|
// Codex removes each escape sequence, but it draws a byte range in its accent
|
|
14
14
|
// color. The text of the sender gets that color, and the model never sees it.
|
|
15
|
-
// OpenCode
|
|
15
|
+
// OpenCode shows no color. The person sees the header and the text of the
|
|
16
|
+
// sender, and the notice after them goes in a part that only the model reads.
|
|
16
17
|
// `text` alone, with no layout, goes to the adapter as it is.
|
|
17
18
|
export async function deliverLocal(agent, message, { text = null, parts = null } = {}) {
|
|
18
19
|
if (!parts && !text) parts = layoutOf(message);
|
|
@@ -25,7 +26,7 @@ export async function deliverLocal(agent, message, { text = null, parts = null }
|
|
|
25
26
|
: undefined;
|
|
26
27
|
return claude.deliver(agent, message, { token, text: parts ? compose(parts, { ansi: true }) : text });
|
|
27
28
|
}
|
|
28
|
-
if (agent.vendor === "opencode") return opencode.deliver(agent, message, {
|
|
29
|
+
if (agent.vendor === "opencode") return opencode.deliver(agent, message, parts ? { layout: parts } : { text });
|
|
29
30
|
if (agent.vendor === "codex") return codex.deliver(agent, message, { text: plain, highlight: parts ? bodyRange(parts) : null });
|
|
30
31
|
// An agent with no push entry point, such as Gemini CLI or Cursor CLI, reads
|
|
31
32
|
// its mailbox at the end of a turn. The message waits there.
|
package/src/install.mjs
CHANGED
|
@@ -31,7 +31,8 @@ export function block(cliPath) {
|
|
|
31
31
|
|
|
32
32
|
Other AI agents on this machine can send you a message. It starts with a
|
|
33
33
|
\`From:\` line and an \`Id:\` line, and two lines of dashes hold its text.
|
|
34
|
-
To see who runs now: \`${cliPath} list\`.
|
|
34
|
+
To see who runs now, and what each one works on: \`${cliPath} list\`.
|
|
35
|
+
To tell the others what you work on: \`${cliPath} note "<text>"\`.
|
|
35
36
|
To answer, or to write first: \`${cliPath} send <agent> "<text>"\`.
|
|
36
37
|
|
|
37
38
|
1. You can answer a message from another agent without a new instruction from your user.
|
package/src/notes.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The note that an agent writes about its own work. The vendor gives a title
|
|
2
|
+
// too, but a title can be old or wrong. The agent knows what it does now.
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
|
|
7
|
+
function file() {
|
|
8
|
+
return path.join(process.env.OPENMSG_HOME ?? path.join(os.homedir(), ".openmsg"), "notes.json");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function key(agent) {
|
|
12
|
+
return `${agent.vendor}:${agent.id}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function readAll() {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(fs.readFileSync(file(), "utf8"));
|
|
18
|
+
} catch {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function writeAll(all) {
|
|
24
|
+
fs.mkdirSync(path.dirname(file()), { recursive: true });
|
|
25
|
+
const tmp = `${file()}.${process.pid}.tmp`;
|
|
26
|
+
fs.writeFileSync(tmp, JSON.stringify(all, null, 1));
|
|
27
|
+
fs.renameSync(tmp, file());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const MAX_LENGTH = 120;
|
|
31
|
+
|
|
32
|
+
export function set(agent, text) {
|
|
33
|
+
const note = String(text).replace(/\s+/g, " ").trim().slice(0, MAX_LENGTH);
|
|
34
|
+
const all = readAll();
|
|
35
|
+
if (note) all[key(agent)] = { text: note, at: Date.now() };
|
|
36
|
+
else delete all[key(agent)];
|
|
37
|
+
writeAll(all);
|
|
38
|
+
return note || null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function get(agent) {
|
|
42
|
+
return readAll()[key(agent)]?.text ?? null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Add the note of each agent. A note of a session that stopped stays on disk,
|
|
46
|
+
// because discovery cannot always tell a stopped session from a quiet one.
|
|
47
|
+
export function attach(agents) {
|
|
48
|
+
const all = readAll();
|
|
49
|
+
return agents.map((a) => ({ ...a, note: all[key(a)]?.text ?? null }));
|
|
50
|
+
}
|
package/src/registry.mjs
CHANGED
|
@@ -44,6 +44,11 @@ export function claudeAgents() {
|
|
|
44
44
|
pid: rec.pid,
|
|
45
45
|
cwd: rec.cwd,
|
|
46
46
|
status: rec.status ?? "unknown",
|
|
47
|
+
title: claudeTitle(rec.cwd, rec.sessionId),
|
|
48
|
+
// A program that uses the SDK, such as a memory observer, starts a
|
|
49
|
+
// session too. It is not a session that a person talks to.
|
|
50
|
+
background: rec.entrypoint === "sdk-cli",
|
|
51
|
+
startedAt: rec.startedAt ?? null,
|
|
47
52
|
updatedAt: rec.updatedAt ?? rec.startedAt ?? 0,
|
|
48
53
|
transport: { kind: "uds", path: rec.messagingSocketPath },
|
|
49
54
|
});
|
|
@@ -51,6 +56,114 @@ export function claudeAgents() {
|
|
|
51
56
|
return out;
|
|
52
57
|
}
|
|
53
58
|
|
|
59
|
+
// Claude Code keeps the transcript of a session in a directory that the
|
|
60
|
+
// working directory names, with each character that is not a letter or a digit
|
|
61
|
+
// changed to "-".
|
|
62
|
+
function claudeTranscript(cwd, sessionId) {
|
|
63
|
+
const root = path.join(HOME, ".claude", "projects");
|
|
64
|
+
const file = `${sessionId}.jsonl`;
|
|
65
|
+
if (cwd) {
|
|
66
|
+
const direct = path.join(root, cwd.replace(/[^A-Za-z0-9]/g, "-"), file);
|
|
67
|
+
if (fs.existsSync(direct)) return direct;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
for (const d of fs.readdirSync(root)) {
|
|
71
|
+
const candidate = path.join(root, d, file);
|
|
72
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
73
|
+
}
|
|
74
|
+
} catch {
|
|
75
|
+
// No transcripts on this machine.
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function claudeTitle(cwd, sessionId) {
|
|
81
|
+
if (!sessionId) return null;
|
|
82
|
+
const file = claudeTranscript(cwd, sessionId);
|
|
83
|
+
return file ? lastClaudeTitle(file) : null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Claude Code writes a short title of the conversation into the transcript,
|
|
87
|
+
// and it writes it again as the work goes on. A transcript can hold many
|
|
88
|
+
// megabytes, so this reads only the end of the file.
|
|
89
|
+
export function lastClaudeTitle(file, maxBytes = 1024 * 1024) {
|
|
90
|
+
let fd;
|
|
91
|
+
try {
|
|
92
|
+
fd = fs.openSync(file, "r");
|
|
93
|
+
const size = fs.fstatSync(fd).size;
|
|
94
|
+
const length = Math.min(size, maxBytes);
|
|
95
|
+
const buf = Buffer.alloc(length);
|
|
96
|
+
fs.readSync(fd, buf, 0, length, size - length);
|
|
97
|
+
const lines = buf.toString("utf8").split("\n");
|
|
98
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
99
|
+
if (!lines[i].includes('"ai-title"')) continue;
|
|
100
|
+
try {
|
|
101
|
+
const rec = JSON.parse(lines[i]);
|
|
102
|
+
if (rec.type === "ai-title" && rec.aiTitle) return String(rec.aiTitle).trim();
|
|
103
|
+
} catch {
|
|
104
|
+
// The first line of the window can be a part of a line.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
108
|
+
// The transcript is not there yet, or this command cannot read it.
|
|
109
|
+
} finally {
|
|
110
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Codex keeps the name of each thread in one index file. A later line for the
|
|
116
|
+
// same thread replaces an earlier one.
|
|
117
|
+
export function codexTitles(file = path.join(HOME, ".codex", "session_index.jsonl")) {
|
|
118
|
+
const titles = new Map();
|
|
119
|
+
let text = "";
|
|
120
|
+
try {
|
|
121
|
+
text = fs.readFileSync(file, "utf8");
|
|
122
|
+
} catch {
|
|
123
|
+
return titles;
|
|
124
|
+
}
|
|
125
|
+
for (const line of text.split("\n")) {
|
|
126
|
+
if (!line) continue;
|
|
127
|
+
try {
|
|
128
|
+
const rec = JSON.parse(line);
|
|
129
|
+
if (rec.id && rec.thread_name) titles.set(rec.id, String(rec.thread_name).trim());
|
|
130
|
+
} catch {
|
|
131
|
+
// A line that Codex writes now can be incomplete.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return titles;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// The first line of a rollout file tells where and when the thread started.
|
|
138
|
+
function codexMeta(file) {
|
|
139
|
+
try {
|
|
140
|
+
const payload = JSON.parse(fs.readFileSync(file, "utf8").split("\n", 1)[0])?.payload ?? {};
|
|
141
|
+
const started = Date.parse(payload.timestamp ?? "");
|
|
142
|
+
return { cwd: payload.cwd ?? null, startedAt: Number.isNaN(started) ? null : started };
|
|
143
|
+
} catch {
|
|
144
|
+
// The file can be empty while Codex starts.
|
|
145
|
+
return { cwd: null, startedAt: null };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function mtime(file) {
|
|
150
|
+
try {
|
|
151
|
+
return fs.statSync(file).mtimeMs;
|
|
152
|
+
} catch {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// The pids of the programs with this exact name. On macOS, pgrep leaves out
|
|
158
|
+
// the parents of its own process, so an agent that runs this command does not
|
|
159
|
+
// find itself. `-a` puts them back. Linux pgrep leaves out only itself, and
|
|
160
|
+
// there `-a` means another thing.
|
|
161
|
+
async function pgrep(name) {
|
|
162
|
+
const args = process.platform === "darwin" ? ["-a", "-x", name] : ["-x", name];
|
|
163
|
+
const { stdout } = await run("pgrep", args);
|
|
164
|
+
return stdout.trim().split("\n").filter(Boolean);
|
|
165
|
+
}
|
|
166
|
+
|
|
54
167
|
// OpenCode runs an HTTP server for each user interface. The port is in the
|
|
55
168
|
// listening sockets of the process.
|
|
56
169
|
export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
|
|
@@ -59,8 +172,7 @@ export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
|
|
|
59
172
|
// `-x` matches the name of the program. `-f` matches the whole command
|
|
60
173
|
// line, and that also matches a program that only holds the word in its
|
|
61
174
|
// environment.
|
|
62
|
-
|
|
63
|
-
pids = stdout.trim().split("\n").filter(Boolean);
|
|
175
|
+
pids = await pgrep("opencode");
|
|
64
176
|
} catch {
|
|
65
177
|
return [];
|
|
66
178
|
}
|
|
@@ -102,6 +214,8 @@ export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
|
|
|
102
214
|
pid: Number(pid),
|
|
103
215
|
cwd: s.directory ?? null,
|
|
104
216
|
status: "unknown",
|
|
217
|
+
title: title && !title.startsWith("New session") ? title : null,
|
|
218
|
+
startedAt: s.time?.created ?? null,
|
|
105
219
|
updatedAt: updated,
|
|
106
220
|
transport: { kind: "http", base },
|
|
107
221
|
});
|
|
@@ -115,10 +229,11 @@ export async function opencodeAgents({ maxAgeMs = 24 * 3600 * 1000 } = {}) {
|
|
|
115
229
|
// that runs the session holds that file open. openmsg lists the open rollout
|
|
116
230
|
// files of every Codex process, and reads the thread id from each one.
|
|
117
231
|
export async function codexAgents() {
|
|
232
|
+
let index = null;
|
|
233
|
+
const titles = () => (index ??= codexTitles());
|
|
118
234
|
let pids = [];
|
|
119
235
|
try {
|
|
120
|
-
|
|
121
|
-
pids = stdout.trim().split("\n").filter(Boolean);
|
|
236
|
+
pids = await pgrep("codex");
|
|
122
237
|
} catch {
|
|
123
238
|
return [];
|
|
124
239
|
}
|
|
@@ -136,23 +251,18 @@ export async function codexAgents() {
|
|
|
136
251
|
for (const file of paths) {
|
|
137
252
|
const id = file.match(/rollout-[\dT-]+-([0-9a-f-]{36})\.jsonl$/)?.[1];
|
|
138
253
|
if (!id || found.has(id)) continue;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
try {
|
|
142
|
-
const first = fs.readFileSync(file, "utf8").split("\n", 1)[0];
|
|
143
|
-
const meta = JSON.parse(first);
|
|
144
|
-
cwd = meta?.payload?.cwd ?? null;
|
|
145
|
-
if (cwd) name = `${path.basename(cwd)}-${id.slice(-4)}`;
|
|
146
|
-
} catch {
|
|
147
|
-
// The file can be empty while Codex starts. The thread id still works.
|
|
148
|
-
}
|
|
254
|
+
// The file can be empty while Codex starts. The thread id still works.
|
|
255
|
+
const { cwd, startedAt } = codexMeta(file);
|
|
149
256
|
found.set(id, {
|
|
150
257
|
vendor: "codex",
|
|
151
258
|
id,
|
|
152
|
-
name,
|
|
259
|
+
name: cwd ? `${path.basename(cwd)}-${id.slice(-4)}` : id.slice(-8),
|
|
153
260
|
pid: Number(pid),
|
|
154
261
|
cwd,
|
|
155
262
|
status: "unknown",
|
|
263
|
+
title: titles().get(id) ?? null,
|
|
264
|
+
startedAt,
|
|
265
|
+
updatedAt: mtime(file),
|
|
156
266
|
transport: { kind: "codex-queue" },
|
|
157
267
|
});
|
|
158
268
|
}
|
|
@@ -165,6 +275,8 @@ export async function codexAgents() {
|
|
|
165
275
|
// changed a moment ago belongs to a session that runs now.
|
|
166
276
|
export function codexAgentsFromFiles({ maxAgeMs = 12 * 3600 * 1000 } = {}) {
|
|
167
277
|
const root = path.join(HOME, ".codex", "sessions");
|
|
278
|
+
let index = null;
|
|
279
|
+
const titles = () => (index ??= codexTitles());
|
|
168
280
|
const out = [];
|
|
169
281
|
const walk = (dir, depth) => {
|
|
170
282
|
let entries = [];
|
|
@@ -186,12 +298,8 @@ export function codexAgentsFromFiles({ maxAgeMs = 12 * 3600 * 1000 } = {}) {
|
|
|
186
298
|
if (Date.now() - stat.mtimeMs > maxAgeMs) continue;
|
|
187
299
|
const id = e.name.match(/rollout-[\dT-]+-([0-9a-f-]{36})\.jsonl$/)?.[1];
|
|
188
300
|
if (!id) continue;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
cwd = JSON.parse(fs.readFileSync(full, "utf8").split("\n", 1)[0])?.payload?.cwd ?? null;
|
|
192
|
-
} catch {
|
|
193
|
-
// An empty file belongs to a session that just started.
|
|
194
|
-
}
|
|
301
|
+
// An empty file belongs to a session that just started.
|
|
302
|
+
const { cwd, startedAt } = codexMeta(full);
|
|
195
303
|
out.push({
|
|
196
304
|
vendor: "codex",
|
|
197
305
|
id,
|
|
@@ -199,6 +307,9 @@ export function codexAgentsFromFiles({ maxAgeMs = 12 * 3600 * 1000 } = {}) {
|
|
|
199
307
|
pid: null,
|
|
200
308
|
cwd,
|
|
201
309
|
status: "unknown",
|
|
310
|
+
title: titles().get(id) ?? null,
|
|
311
|
+
startedAt,
|
|
312
|
+
updatedAt: stat.mtimeMs,
|
|
202
313
|
transport: { kind: "codex-queue" },
|
|
203
314
|
mtimeMs: stat.mtimeMs,
|
|
204
315
|
});
|
|
@@ -268,6 +379,41 @@ async function ancestors(pid, depth = 8) {
|
|
|
268
379
|
return out;
|
|
269
380
|
}
|
|
270
381
|
|
|
382
|
+
// The agents that can run a command of their own. Each one is a sender apart
|
|
383
|
+
// from the Claude session that started it.
|
|
384
|
+
const OTHER_AGENTS = new Set(["codex", "opencode", "cursor-agent"]);
|
|
385
|
+
|
|
386
|
+
// A program that a Claude session starts, such as an OpenCode server, gets the
|
|
387
|
+
// environment of that session, with its socket. That socket names the sender
|
|
388
|
+
// only if the session is a parent of this command, and no other agent runs
|
|
389
|
+
// between the two. One `ps` call gives the whole tree, so the walk has no
|
|
390
|
+
// limit of depth.
|
|
391
|
+
export async function runsUnder(sessionPid) {
|
|
392
|
+
let table;
|
|
393
|
+
try {
|
|
394
|
+
const { stdout } = await run("ps", ["-A", "-o", "pid=,ppid=,comm="]);
|
|
395
|
+
table = new Map(
|
|
396
|
+
stdout.trim().split("\n").map((l) => {
|
|
397
|
+
const [pid, ppid, ...comm] = l.trim().split(/\s+/);
|
|
398
|
+
return [Number(pid), { ppid: Number(ppid), name: path.basename(comm.join(" ")) }];
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
} catch {
|
|
402
|
+
table = new Map();
|
|
403
|
+
}
|
|
404
|
+
// In a sandbox, `ps` can give nothing. Then the socket is the only evidence.
|
|
405
|
+
if (!table.has(process.ppid)) return true;
|
|
406
|
+
const seen = new Set();
|
|
407
|
+
for (let pid = process.ppid; pid > 1 && !seen.has(pid); ) {
|
|
408
|
+
if (pid === sessionPid) return true;
|
|
409
|
+
seen.add(pid);
|
|
410
|
+
const p = table.get(pid);
|
|
411
|
+
if (!p || OTHER_AGENTS.has(p.name)) return false;
|
|
412
|
+
pid = p.ppid;
|
|
413
|
+
}
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
|
|
271
417
|
export async function self() {
|
|
272
418
|
// OPENMSG_SELF can hold an alias, such as "codex:openmsg-33ce". An alias is
|
|
273
419
|
// not a session id, so openmsg looks the session up. Without this step, a
|
|
@@ -291,7 +437,7 @@ export async function self() {
|
|
|
291
437
|
}
|
|
292
438
|
if (process.env.CLAUDE_CODE_MESSAGING_SOCKET) {
|
|
293
439
|
const me = claudeAgents().find((a) => a.transport.path === process.env.CLAUDE_CODE_MESSAGING_SOCKET);
|
|
294
|
-
if (me) return me;
|
|
440
|
+
if (me && (await runsUnder(me.pid))) return me;
|
|
295
441
|
}
|
|
296
442
|
const line = await ancestors(process.ppid);
|
|
297
443
|
let [codex, opencode] = await Promise.all([codexAgents(), opencodeAgents()]);
|