@conveno/cli 0.2.0-rc.11 → 0.2.0-rc.13

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.
@@ -4,18 +4,18 @@ import { homedir } from "node:os";
4
4
  import { delimiter, join, win32 } from "node:path";
5
5
  import { createInterface } from "node:readline";
6
6
  import { fileURLToPath } from "node:url";
7
- import { n as antigravityAdapter } from "./dist-KWlFKO7H.js";
8
- import { a as opencodeAdapter, o as claudeAdapter, r as readAllStdin } from "./hook-entry-BYvXWDJv.js";
9
- import { t as codexAdapter } from "./dist-DEjU_KXC.js";
10
- import { t as grokAdapter } from "./dist-Y0JDSJP_.js";
7
+ import { n as antigravityAdapter } from "./dist-D4sb5RQD.js";
8
+ import { a as opencodeAdapter, o as claudeAdapter, r as readAllStdin } from "./hook-entry-WlJSM5aD.js";
9
+ import { t as codexAdapter } from "./dist-C9Z1EMlM.js";
10
+ import { t as grokAdapter } from "./dist-B14ldKRS.js";
11
11
  import { t as createTheme } from "./dist-BRjg9CLd.js";
12
- import { n as connect$1 } from "./dist-n6XMTlyN.js";
13
- import { Bn as number, Dt as readCommandLine, Et as readAncestry, Fn as _enum, Hn as string, In as array, Ln as boolean, P as dataDir, R as detectRepo, Rt as resolveWindowsCommand, S as baseName, T as claimSpawnRecord, Wt as spawnSession, Y as formatInbox, at as launcherText, dn as MessageId, en as validateRename, f as SpawnError, fn as OWNER_SESSION_ID, hn as RpcErrorCode, ht as parsePs1ShimTarget, in as CLI_KINDS, it as launcherPath, jt as realSpawnDeps, lt as needsQuoting, mt as parseCmdShimTarget, o as HEARTBEAT_INTERVAL_MS, r as ConvenoError, s as NOT_FROM_USER_NOTICE, u as SPAWN_CLIS, vn as SessionId, xt as planSpawn, y as attachAcpSession } from "./dist-qONHJRJk.js";
14
- import "./dist-Dg2IY9OA.js";
12
+ import { n as connect$1 } from "./dist-Ca3P7aN-.js";
13
+ import { $ as formatInbox, C as attachAcpSession, E as baseName, H as detectRepo, Mn as SessionId, Nt as planSpawn, On as RpcErrorCode, Ot as pendingTurn, R as dataDir, Rt as readAncestry, Tn as OWNER_SESSION_ID, Tt as parsePs1ShimTarget, Wt as realSpawnDeps, Xn as array, Yn as _enum, Zn as boolean, Zt as resolveWindowsCommand, _n as CLI_KINDS, a as ConvenoError, c as HEARTBEAT_INTERVAL_MS, er as number, f as SPAWN_CLIS, k as claimSpawnRecord, kt as pidAlive, l as NOT_FROM_USER_NOTICE, m as SpawnError, mt as launcherText, nr as string, pn as validateRename, pt as launcherPath, rn as spawnSession, st as isAntigravityApp, vt as needsQuoting, wn as MessageId, wt as parseCmdShimTarget, zt as readCommandLine } from "./dist-DnNuDzEA.js";
14
+ import "./dist-CoRBNycn.js";
15
15
  import { access } from "node:fs/promises";
16
16
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
17
17
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
18
- // ../daemon/dist/server-Dh2GFVxr.js
18
+ // ../daemon/dist/server-D8Ib1txr.js
19
19
  /** `kill(pid, 0)` liveness: EPERM means the process exists but belongs to someone else. */
20
20
  function defaultIsAlive(pid) {
21
21
  try {
@@ -28,7 +28,7 @@ function defaultIsAlive(pid) {
28
28
  `${OWNER_SESSION_ID}`;
29
29
  SessionId.parse("0".repeat(26));
30
30
  // ../mcp/dist/index.js
31
- const SERVER_VERSION = "0.2.0-rc.11";
31
+ const SERVER_VERSION = "0.2.0-rc.13";
32
32
  const ok = (text) => ({ content: [{
33
33
  type: "text",
34
34
  text
@@ -106,6 +106,8 @@ function whoamiText(s) {
106
106
  }
107
107
  /** The daemon no longer knows this session (restart, pruned, name taken). Recipient errors do not match. */
108
108
  const isSessionGone = (err) => err instanceof ConvenoError && err.code === RpcErrorCode.NotFound && /No session with id|not registered|Register the session again/.test(err.message);
109
+ /** The daemon retired this session: it will not take heartbeats from it again. */
110
+ const isSessionEnded = (err) => err instanceof ConvenoError && err.code === RpcErrorCode.NotFound && err.message.includes("has ended and will not come back");
109
111
  const SESSION_LOST_TEXT = "Conveno lost this session and could not restore it automatically. Tell the user that Conveno is not available in this session right now; restarting the CLI session reconnects it.";
110
112
  /**
111
113
  * Sent in the `instructions` field of the initialize result. MCP clients put it in the model's
@@ -140,25 +142,22 @@ function createServer(opts) {
140
142
  /** This server acts as a session an ACP host registered, and leaves its lifecycle to the host. */
141
143
  let attached = false;
142
144
  /**
143
- * OpenCode 2.x serves several sessions from one MCP process. Each OpenCode session that calls a
144
- * tool gets its own Conveno session; the first one takes over the session this process
145
- * registered at start, the others register fresh and heartbeat on their own.
145
+ * The id Conveno acts as. It starts as the session this process registered, and changes to the
146
+ * surviving session when `session.bind` merges this process's session into the one that already
147
+ * holds its conversation (Codex runs one MCP server per thread, so a thread the window already
148
+ * has may be registered a second time). Heartbeats, tool calls and unregister all use it.
146
149
  */
147
- const byOpencode = /* @__PURE__ */ new Map();
148
- const extraBeats = /* @__PURE__ */ new Map();
149
- let baseOwner;
150
- /** Stop an extra session's heartbeat and forget it, so a closed OpenCode session goes offline. */
151
- const release = (ocId, id) => {
152
- const beat = extraBeats.get(id);
153
- if (beat) clearInterval(beat);
154
- extraBeats.delete(id);
155
- byOpencode.delete(ocId);
156
- };
157
- const register = async (extra = false, ocId) => {
150
+ let actingId;
151
+ /** The conversation this process last bound, so a tool call binds only when it changes. */
152
+ let boundConversation;
153
+ let binding = Promise.resolve();
154
+ const isAlive = opts.isAlive ?? pidAlive;
155
+ const parentGone = () => opts.parentPid !== void 0 && !isAlive(opts.parentPid);
156
+ const register = async () => {
158
157
  const repo = await detectRepo(cwd, { exists: pathExists });
159
158
  const cliVersion = process.env.CONVENO_CLI_VERSION?.trim().slice(0, 64);
160
- const { hostPid, hostPids } = await host;
161
- const record = extra ? void 0 : claimSpawnRecord(spawnDataDir, cli, hostPids);
159
+ const { hostPid, hostPids, variant } = await host;
160
+ const record = claimSpawnRecord(spawnDataDir, cli, hostPids);
162
161
  if (record?.acp) {
163
162
  attached = true;
164
163
  return attachAcpSession(client, spawnDataDir, record);
@@ -171,35 +170,73 @@ function createServer(opts) {
171
170
  ...hostPid !== void 0 ? { hostPid } : {},
172
171
  ...hostPids.length > 0 ? { hostPids: hostPids.slice(0, 8) } : {},
173
172
  ...cliVersion ? { cliVersion } : {},
173
+ ...variant ? { variant } : {},
174
174
  ...repo.root ? { repo: { root: repo.root } } : {},
175
175
  ...spawnedBy.success ? { spawnedBy: spawnedBy.data } : {},
176
176
  ...record ? { spawned: true } : {}
177
177
  };
178
- const res = await (record ? client["session.register"]({
178
+ let session = (await (record ? client["session.register"]({
179
179
  ...params,
180
180
  name: record.name
181
181
  }).catch((err) => {
182
182
  if (err instanceof ConvenoError && err.code === RpcErrorCode.Conflict) return client["session.register"](params);
183
183
  throw err;
184
- }) : client["session.register"](params));
185
- const id = res.session.id;
184
+ }) : client["session.register"](params))).session;
185
+ actingId = session.id;
186
+ boundConversation = session.nativeId;
186
187
  const claudeId = cli === "claude" ? process.env.CLAUDE_CODE_SESSION_ID?.trim() : void 0;
187
- if (!extra && claudeId) await client["session.setNative"]({
188
- sessionId: id,
189
- nativeId: claudeId
190
- }).catch(() => {});
188
+ if (claudeId && session.nativeId === void 0) {
189
+ const bound = await bindTo(claudeId, session);
190
+ if (bound) session = bound;
191
+ if (await pendingTurn(spawnDataDir, "claude-pending-turn").take(claudeId)) await client["turn.start"]({ sessionId: session.id }).catch(() => {});
192
+ }
191
193
  const beat = setInterval(() => {
192
- client["session.heartbeat"]({ sessionId: id }).catch((err) => {
193
- if (extra && ocId !== void 0 && isSessionGone(err)) release(ocId, id);
194
+ if (!actingId) return;
195
+ if (parentGone()) {
196
+ shutdown().then(() => opts.onExit?.());
197
+ return;
198
+ }
199
+ client["session.heartbeat"]({
200
+ sessionId: actingId,
201
+ pid: process.pid
202
+ }).catch((err) => {
203
+ if (isSessionEnded(err)) {
204
+ if (timer) clearInterval(timer);
205
+ timer = void 0;
206
+ opts.onExit?.();
207
+ return;
208
+ }
209
+ if (!isSessionGone(err)) return;
210
+ forgetSession();
211
+ ensure().catch(() => {});
194
212
  });
195
213
  }, HEARTBEAT_INTERVAL_MS);
196
214
  beat.unref();
197
- if (extra) extraBeats.set(id, beat);
198
- else {
199
- if (timer) clearInterval(timer);
200
- timer = beat;
215
+ if (timer) clearInterval(timer);
216
+ timer = beat;
217
+ return session;
218
+ };
219
+ /**
220
+ * Bind this process's session to a conversation. The daemon may answer with another session, the
221
+ * one that already holds it; this process acts as that one from then on. Best effort: without
222
+ * the answer the session stays as it is.
223
+ */
224
+ const bindTo = async (conversation, me) => {
225
+ try {
226
+ const { session } = await client["session.bind"]({
227
+ cli,
228
+ nativeId: conversation,
229
+ sessionId: me.id,
230
+ pid: process.pid,
231
+ turnStart: true
232
+ });
233
+ if (!session) return void 0;
234
+ boundConversation = conversation;
235
+ actingId = session.id;
236
+ return session;
237
+ } catch {
238
+ return;
201
239
  }
202
- return res.session;
203
240
  };
204
241
  const ensure = () => {
205
242
  if (!registering) {
@@ -211,20 +248,15 @@ function createServer(opts) {
211
248
  }
212
249
  return registering;
213
250
  };
214
- /** Forget a dead session so the next ensure() registers again. Only drops `dead`, not a newer one. */
215
- const forget = (dead) => {
216
- if (registering === dead) registering = void 0;
217
- };
218
251
  const shutdown = () => {
219
252
  shutting ??= (async () => {
220
253
  if (timer) clearInterval(timer);
221
254
  timer = void 0;
222
255
  const s = await registering?.catch(() => void 0);
223
- if (s && !attached) await client["session.unregister"]({ sessionId: s.id }).catch(() => void 0);
224
- for (const beat of extraBeats.values()) clearInterval(beat);
225
- extraBeats.clear();
226
- const others = await Promise.all([...byOpencode.values()].map((p) => p.catch(() => void 0)));
227
- for (const o of others) if (o && o.id !== s?.id) await client["session.unregister"]({ sessionId: o.id }).catch(() => void 0);
256
+ if (s && !attached) await client["session.unregister"]({
257
+ sessionId: actingId ?? s.id,
258
+ pid: process.pid
259
+ }).catch(() => void 0);
228
260
  })();
229
261
  return shutting;
230
262
  };
@@ -239,44 +271,38 @@ function createServer(opts) {
239
271
  await closeTransport();
240
272
  await shutdown();
241
273
  };
242
- /** The Conveno session for an OpenCode session id; without one, this process's own session. */
243
- const sessionFor = (ocId) => {
244
- if (!ocId) return ensure();
245
- let p = byOpencode.get(ocId);
246
- if (!p) {
247
- const takeBase = baseOwner === void 0;
248
- if (takeBase) baseOwner = ocId;
249
- const made = (takeBase ? ensure() : register(true, ocId)).then(async (s) => {
250
- await client["session.setNative"]({
251
- sessionId: s.id,
252
- nativeId: ocId
253
- });
254
- return s;
255
- });
256
- p = made;
257
- byOpencode.set(ocId, made);
258
- made.catch(() => {
259
- if (byOpencode.get(ocId) === made) byOpencode.delete(ocId);
260
- if (takeBase && baseOwner === ocId) baseOwner = void 0;
261
- });
262
- }
263
- return p;
274
+ /**
275
+ * The session a tool call acts as. A call that names its conversation (`_meta`) is a turn start
276
+ * in it: when it differs from the one this process last bound, the window's session moves to it
277
+ * (and a session of another process that already holds it takes this one over).
278
+ */
279
+ const sessionFor = async (conversation) => {
280
+ const me = await ensure();
281
+ if (!conversation || attached || conversation === boundConversation) return me;
282
+ const turn = binding.then(async () => {
283
+ const current = await ensure();
284
+ if (conversation === boundConversation) return current;
285
+ const bound = await bindTo(conversation, current);
286
+ if (bound) registering = Promise.resolve(bound);
287
+ return bound ?? current;
288
+ });
289
+ binding = turn.catch(() => void 0);
290
+ return turn;
264
291
  };
265
292
  /** Drop a dead session so the next call registers again. */
266
- const forgetFor = (ocId, dead) => {
267
- if (!ocId) {
268
- forget(dead);
269
- return;
270
- }
271
- if (byOpencode.get(ocId) === dead) byOpencode.delete(ocId);
272
- if (baseOwner === ocId) {
273
- baseOwner = void 0;
274
- registering = void 0;
275
- }
293
+ const forgetSession = () => {
294
+ registering = void 0;
295
+ boundConversation = void 0;
296
+ actingId = void 0;
276
297
  };
277
- /** OpenCode puts the calling session's id in the request's `_meta`. */
278
- const opencodeId = (extra) => {
279
- const v = extra?._meta?.["ai.opencode/sessionID"];
298
+ /**
299
+ * The conversation a tool call comes from, in the request's `_meta`: Codex `threadId`, OpenCode
300
+ * `ai.opencode/sessionID`, Antigravity `antigravity.google/root_conversation_id`.
301
+ */
302
+ const conversationOf = (extra) => {
303
+ const meta = extra?._meta;
304
+ const key = cli === "codex" ? "threadId" : cli === "opencode" ? "ai.opencode/sessionID" : cli === "antigravity" ? "antigravity.google/root_conversation_id" : void 0;
305
+ const v = key === void 0 ? void 0 : meta?.[key];
280
306
  return typeof v === "string" && v.length > 0 && v.length <= 256 ? v : void 0;
281
307
  };
282
308
  /**
@@ -287,16 +313,15 @@ function createServer(opts) {
287
313
  const tool = (body) => async (...given) => {
288
314
  const extra = given.length > 1 ? given[given.length - 1] : void 0;
289
315
  const args = given.length > 1 ? given[0] : {};
290
- const oc = opencodeId(extra ?? given[0]);
291
- const first = sessionFor(oc);
316
+ const conversation = conversationOf(extra ?? given[0]);
292
317
  try {
293
- return ok(await body(await first, args));
318
+ return ok(await body(await sessionFor(conversation), args));
294
319
  } catch (err) {
295
320
  if (!isSessionGone(err)) return fail(err);
296
- forgetFor(oc, first);
321
+ forgetSession();
297
322
  }
298
323
  try {
299
- return ok(await body(await sessionFor(oc), args));
324
+ return ok(await body(await sessionFor(conversation), args));
300
325
  } catch (err) {
301
326
  return isSessionGone(err) ? fail(/* @__PURE__ */ new Error(SESSION_LOST_TEXT)) : fail(err);
302
327
  }
@@ -478,7 +503,8 @@ async function runStdio(opts) {
478
503
  launcherPaths: process.argv[1] ? [process.argv[1]] : []
479
504
  }).then((a) => ({
480
505
  hostPid: a.hostPid,
481
- hostPids: a.pids
506
+ hostPids: a.pids,
507
+ variant: opts.cli === "antigravity" && isAntigravityApp(a.entries) ? "app" : void 0
482
508
  }));
483
509
  const client = await connect$1({
484
510
  kind: "mcp",
@@ -489,7 +515,9 @@ async function runStdio(opts) {
489
515
  cli: opts.cli,
490
516
  client,
491
517
  host,
492
- cwd: process.cwd()
518
+ cwd: process.cwd(),
519
+ ...process.ppid > 1 ? { parentPid: process.ppid } : {},
520
+ onExit: () => void stop().then(() => process.exit(0))
493
521
  });
494
522
  let stopping;
495
523
  const stop = () => {