baychat 0.33.2 → 0.34.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.
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ownSessionName = ownSessionName;
37
+ exports.stopRegistered = stopRegistered;
38
+ exports.realStopRegisteredDeps = realStopRegisteredDeps;
39
+ const child_process_1 = require("child_process");
40
+ const fs = __importStar(require("fs"));
41
+ const terminal_pane_1 = require("../terminal-pane");
42
+ const argv_1 = require("./argv");
43
+ const multiplexer_1 = require("./multiplexer");
44
+ /**
45
+ * The tmux session names our CLI has ever given, for session `name` (git history of
46
+ * multiplexer-launch.ts and launcher/argv.ts):
47
+ * - `bc-<name>` — the relay's launcher (a session started from BayChat);
48
+ * - `claude-<pid>`, `claude-<pid>-<n>`, `claude-<pid>-<epoch ms>` — `baychat claude` (`fresh`);
49
+ * - `<name>`, or `baychat` when no name was given — `baychat start [name]` (`reattach`).
50
+ * The last two are names a person could give their own tmux too, so they count only when the
51
+ * session recorded that our CLI made it (`recorded`); without a record only the first two do.
52
+ */
53
+ function ownSessionName(tmuxSession, name, recorded) {
54
+ if (recorded !== undefined) {
55
+ if (tmuxSession !== recorded)
56
+ return false;
57
+ return ownSessionName(tmuxSession, name) || tmuxSession === name || tmuxSession === "baychat";
58
+ }
59
+ return tmuxSession === `${argv_1.LAUNCH_SESSION_PREFIX}${name}` || /^claude-\d+(-\d+)?$/.test(tmuxSession);
60
+ }
61
+ const PANE_FORMAT = "#{pane_id}\t#{session_name}\t#{window_panes}\t#{session_windows}\t#{pid}\t#{pane_tty}";
62
+ function lookPane(handle, run) {
63
+ const res = run("tmux", ["-S", handle.socket, "display-message", "-p", "-t", handle.pane, PANE_FORMAT]);
64
+ if (res.error)
65
+ return { unreachable: res.error.message };
66
+ if (res.status !== 0) {
67
+ // "can't find pane" = the server answered and the pane is gone. Anything else (no server on
68
+ // that socket, the socket missing) is not knowledge that it is gone.
69
+ return /can't find|no such pane/i.test(res.stderr) ? { gone: true } : { unreachable: res.stderr.trim() || `exit ${res.status}` };
70
+ }
71
+ const [paneId = "", session = "", panes = "", windows = "", pid = "", tty = ""] = res.stdout.trim().split("\t");
72
+ return { found: { paneId, session, windowPanes: Number(panes), sessionWindows: Number(windows), serverPid: Number(pid), tty } };
73
+ }
74
+ /** `lookPane`, after making the server's socket again when macOS's tmp cleanup deleted it
75
+ * (multiplexer.ts `healTmuxSocket`): without that, a live pane would read as unreachable. */
76
+ async function lookPaneHealing(handle, deps) {
77
+ const first = lookPane(handle, deps.run);
78
+ if (!("unreachable" in first))
79
+ return first;
80
+ // "no-server" is NOT "gone": a server we could not recognise may still run the agent, and saying
81
+ // "closed" for a terminal that is still open is the one answer End must never give.
82
+ let healed = deps.heal(handle.socket, handle.serverPid);
83
+ for (let i = 0; healed === "signalled" && i < 10; i++) {
84
+ await deps.sleep(100);
85
+ if (deps.socketExists(handle.socket))
86
+ healed = "healed";
87
+ }
88
+ if (healed !== "healed")
89
+ return first;
90
+ deps.log(`tmux socket ${handle.socket} was missing; the tmux server made it again`);
91
+ return lookPane(handle, deps.run);
92
+ }
93
+ /** Is `pid` alive with its controlling terminal `tty` (ps prints `ttys003`, tmux `/dev/ttys003`)? */
94
+ function aliveOn(pid, tty, check) {
95
+ try {
96
+ const own = check("ps", ["-o", "tty=", "-p", String(pid)]).trim();
97
+ return !!own && own !== "?" && (tty === own || tty.endsWith(`/${own}`));
98
+ }
99
+ catch {
100
+ return false;
101
+ }
102
+ }
103
+ async function stopRegistered(name, target, deps) {
104
+ if (!target?.pane)
105
+ return "not-registered";
106
+ const handle = target.pane;
107
+ // screen and zellij cannot name the session a window is in, so check 2 is impossible: refused.
108
+ if (handle.kind !== "tmux")
109
+ return "not-ours";
110
+ // Without the server's pid a pane id proves nothing (it may be a reused %N): refused.
111
+ if (!handle.serverPid)
112
+ return "not-ours";
113
+ const look = await lookPaneHealing(handle, deps);
114
+ if ("gone" in look)
115
+ return "not-running";
116
+ if ("unreachable" in look) {
117
+ deps.log(`stop "${name}": tmux at ${handle.socket} did not answer (${look.unreachable})`);
118
+ return "unreachable";
119
+ }
120
+ const pane = look.found;
121
+ if (pane.paneId !== handle.pane || pane.serverPid !== handle.serverPid) {
122
+ deps.log(`stop "${name}": pane ${handle.pane} now belongs to another tmux server; left open`);
123
+ return "not-ours";
124
+ }
125
+ if (!ownSessionName(pane.session, name, handle.session)) {
126
+ deps.log(`stop "${name}": pane ${handle.pane} is in tmux session "${pane.session}", which BayChat did not make; left open`);
127
+ return "not-ours";
128
+ }
129
+ const running = (0, terminal_pane_1.verifyPane)(handle, target.runtime, deps.check, target.ownerPid);
130
+ if (!running.ok) {
131
+ deps.log(`stop "${name}": ${running.reason} Left open.`);
132
+ return "not-ours";
133
+ }
134
+ if (target.ownerPid !== undefined && !aliveOn(target.ownerPid, pane.tty, deps.check)) {
135
+ deps.log(`stop "${name}": its process ${target.ownerPid} is not on pane ${handle.pane} any more; left open`);
136
+ return "not-ours";
137
+ }
138
+ const whole = pane.windowPanes === 1 && pane.sessionWindows === 1;
139
+ deps.run("tmux", whole ? ["-S", handle.socket, "kill-session", "-t", `=${pane.session}`] : ["-S", handle.socket, "kill-pane", "-t", handle.pane]);
140
+ // The kill's own answer is not the proof (handle.ts `stopRecorded`): the pane must be GONE.
141
+ if (!("gone" in lookPane(handle, deps.run)))
142
+ return "failed";
143
+ deps.log(`stop "${name}": closed ${whole ? `tmux session "${pane.session}"` : `pane ${handle.pane} of "${pane.session}"`}`);
144
+ return "stopped";
145
+ }
146
+ function realStopRegisteredDeps(run, log) {
147
+ return {
148
+ run,
149
+ check: (file, args) => (0, child_process_1.execFileSync)(file, args, { encoding: "utf8", timeout: 5_000 }),
150
+ heal: (socket, serverPid) => (0, multiplexer_1.healTmuxSocket)(run, { socket, serverPid }),
151
+ socketExists: (socket) => fs.existsSync(socket),
152
+ sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
153
+ log,
154
+ };
155
+ }
@@ -23,9 +23,18 @@ function detectPane(env = process.env) {
23
23
  const tmuxPane = env.TMUX_PANE;
24
24
  if (tmux && tmuxPane) {
25
25
  // "<socket>,<server pid>,<session>" — only the first field addresses the server.
26
- const socket = tmux.split(",")[0];
27
- if (socket)
28
- return { kind: "tmux", socket, pane: tmuxPane };
26
+ const [socket, pid] = tmux.split(",");
27
+ const serverPid = Number(pid);
28
+ const session = env.BAYCHAT_TMUX_SESSION;
29
+ if (socket) {
30
+ return {
31
+ kind: "tmux",
32
+ socket,
33
+ pane: tmuxPane,
34
+ ...(Number.isInteger(serverPid) && serverPid > 0 ? { serverPid } : {}),
35
+ ...(session ? { session } : {}),
36
+ };
37
+ }
29
38
  }
30
39
  if (env.STY)
31
40
  return { kind: "screen", session: env.STY, window: env.WINDOW ?? "0" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baychat",
3
- "version": "0.33.2",
3
+ "version": "0.34.0",
4
4
  "description": "BayChat connector CLI — pair an agent session (Claude Code, Codex, Cursor) with BayChat, or have the relay run DeepSeek or the Cursor CLI for you",
5
5
  "bin": {
6
6
  "baychat": "dist/index.js"