tmux-ide 2.9.23 → 2.9.25
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/bin/cli.js +1636 -385
- package/package.json +4 -2
- package/packages/contracts/src/pane-stream.ts +53 -0
- package/packages/daemon/dist/command-center/discovery.js +24 -5
- package/packages/daemon/dist/command-center/server.js +81 -75
- package/packages/daemon/dist/command-center/tmux-servers.js +11 -0
- package/packages/daemon/dist/doctor.js +46 -10
- package/packages/daemon/dist/lib/canonical-daemon-bootstrap.js +20 -2
- package/packages/daemon/dist/lib/daemon-crash-snapshot.js +118 -0
- package/packages/daemon/dist/lib/daemon-embed.js +22 -8
- package/packages/daemon/dist/lib/headless-daemon.js +63 -6
- package/packages/daemon/dist/lib/home-agent-observer.js +50 -0
- package/packages/daemon/dist/lib/invocation-directory.js +20 -0
- package/packages/daemon/dist/lib/machine-agents.js +18 -1
- package/packages/daemon/dist/lib/orphaned-control-clients.js +162 -0
- package/packages/daemon/dist/lib/runtime-namespace.js +22 -6
- package/packages/daemon/dist/lib/tmux-client-execution.js +12 -3
- package/packages/daemon/dist/lib/tmux-external-interaction-observer.js +32 -9
- package/packages/daemon/dist/lib/tmux-server-owner.js +26 -4
- package/packages/daemon/dist/lib/update-check.js +18 -0
- package/packages/daemon/dist/lib/update-daemon-outcome.js +31 -0
- package/packages/daemon/dist/lib/update.js +39 -14
- package/packages/daemon/dist/lib/workspace-promotion.js +24 -3
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
- package/packages/daemon/dist/terminal/attachments/native-runtime.js +17 -2
- package/packages/daemon/dist/terminal/graphics/kitty-graphics.js +16 -4
- package/packages/daemon/dist/terminal/mirror/mirror-service.js +26 -0
- package/packages/daemon/dist/terminal/mirror/session-channel.js +27 -2
- package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +749 -208
- package/packages/daemon/dist/terminal/session-runtime/registry.js +24 -2
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-interpreter.js +77 -2
- package/packages/daemon/dist/terminal/session-runtime/terminal-replica-owner.js +24 -2
- package/packages/daemon/dist/terminal/session-runtime/transport-binding.js +38 -0
- package/packages/daemon/dist/tui/chrome/snapshot.js +36 -1
- package/packages/daemon/dist/tui/chrome/statusline.js +8 -5
- package/packages/daemon/dist/tui/chrome/updater.js +2 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-navigation.js +57 -4
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +18 -7
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-agent-navigation.js +17 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-navigation.js +4 -2
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-interaction-controller.js +55 -14
- package/packages/daemon/src/command-center/discovery.ts +24 -4
- package/packages/daemon/src/command-center/server.ts +67 -67
- package/packages/daemon/src/command-center/tmux-servers.ts +11 -0
- package/packages/daemon/src/doctor.ts +50 -9
- package/packages/daemon/src/lib/canonical-daemon-bootstrap.ts +31 -5
- package/packages/daemon/src/lib/daemon-crash-snapshot.ts +175 -0
- package/packages/daemon/src/lib/daemon-embed.ts +74 -7
- package/packages/daemon/src/lib/headless-daemon.ts +88 -7
- package/packages/daemon/src/lib/home-agent-observer.ts +53 -0
- package/packages/daemon/src/lib/invocation-directory.ts +21 -0
- package/packages/daemon/src/lib/machine-agents.ts +25 -1
- package/packages/daemon/src/lib/orphaned-control-clients.ts +215 -0
- package/packages/daemon/src/lib/runtime-namespace.ts +25 -7
- package/packages/daemon/src/lib/tmux-client-execution.ts +14 -5
- package/packages/daemon/src/lib/tmux-external-interaction-observer.ts +32 -8
- package/packages/daemon/src/lib/tmux-server-owner.ts +28 -3
- package/packages/daemon/src/lib/update-check.ts +34 -0
- package/packages/daemon/src/lib/update-daemon-outcome.ts +42 -0
- package/packages/daemon/src/lib/update.ts +58 -16
- package/packages/daemon/src/lib/workspace-promotion.ts +26 -3
- package/packages/daemon/src/terminal/attachments/native-runtime.ts +21 -2
- package/packages/daemon/src/terminal/graphics/kitty-graphics.ts +22 -6
- package/packages/daemon/src/terminal/mirror/mirror-service.ts +27 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +32 -2
- package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +819 -223
- package/packages/daemon/src/terminal/session-runtime/registry.ts +35 -2
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-interpreter.ts +74 -2
- package/packages/daemon/src/terminal/session-runtime/terminal-replica-owner.ts +21 -1
- package/packages/daemon/src/terminal/session-runtime/transport-binding.ts +42 -2
- package/packages/daemon/src/tui/chrome/snapshot.ts +47 -1
- package/packages/daemon/src/tui/chrome/statusline.ts +7 -4
- package/packages/daemon/src/tui/chrome/updater.ts +3 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-navigation.ts +69 -4
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +30 -13
- package/packages/daemon/src/tui/mirror/runtime/application-machine-agent-navigation.ts +38 -1
- package/packages/daemon/src/tui/mirror/runtime/application-machine-navigation.ts +7 -2
- package/packages/daemon/src/tui/mirror/runtime/application-root-v2.tsx +4 -4
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-interaction-controller.ts +71 -17
- package/packages/daemon-client/src/pane-stream-client.ts +47 -5
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { logger } from "./log.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A daemon that dies without running its stop (SIGKILL, a crash) can leave a
|
|
6
|
+
* tmux control client behind. Measured on a private server: with pane output
|
|
7
|
+
* flowing and the daemon stopped for a few seconds before the kill, the
|
|
8
|
+
* client stays alive with init as its parent. tmux no longer lists it as a
|
|
9
|
+
* client, yet a later `kill-server` never finishes while it lives: the socket
|
|
10
|
+
* keeps a server with no sessions and no new server can take its place.
|
|
11
|
+
* A daemon therefore ends the control clients a dead daemon left on a server
|
|
12
|
+
* it owns: when it takes ownership, and when that server answers a catalog
|
|
13
|
+
* read with no sessions or does not answer it at all.
|
|
14
|
+
*/
|
|
15
|
+
export type TmuxServerSelector =
|
|
16
|
+
| { readonly kind: "path"; readonly path: string }
|
|
17
|
+
| { readonly kind: "name"; readonly name: string };
|
|
18
|
+
|
|
19
|
+
export interface ProcessRow {
|
|
20
|
+
readonly pid: number;
|
|
21
|
+
readonly ppid: number;
|
|
22
|
+
readonly command: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface OrphanedControlClientIo {
|
|
26
|
+
/** Every process of this user: tmux does not list a client that is leaving. */
|
|
27
|
+
readonly listProcesses: () => Promise<readonly ProcessRow[]>;
|
|
28
|
+
kill: (pid: number, signal: "SIGTERM" | "SIGKILL") => void;
|
|
29
|
+
readonly isAlive: (pid: number) => boolean;
|
|
30
|
+
readonly delay: (ms: number) => Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const ORPHAN_KILL_GRACE_MS = 500;
|
|
34
|
+
|
|
35
|
+
/** PURE. `ps -o pid=,ppid=,command=` output, one process per line. */
|
|
36
|
+
export function parseProcessTable(text: string): ProcessRow[] {
|
|
37
|
+
const rows: ProcessRow[] = [];
|
|
38
|
+
for (const line of text.split("\n")) {
|
|
39
|
+
const match = /^\s*([0-9]+)\s+([0-9]+)\s+(.+)$/u.exec(line);
|
|
40
|
+
if (match) rows.push({ pid: Number(match[1]), ppid: Number(match[2]), command: match[3]! });
|
|
41
|
+
}
|
|
42
|
+
return rows;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The attach flags only this daemon's control client uses (mirrorControlAttachArgs). */
|
|
46
|
+
export function isTmuxIdeControlClientCommand(command: string): boolean {
|
|
47
|
+
return (
|
|
48
|
+
/(?:^|\s)-C(?:\s|$)/u.test(command) &&
|
|
49
|
+
/ignore-size,pause-after=[0-9]+,active-pane/u.test(command)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const withoutPrivate = (path: string): string => path.replace(/^\/private(?=\/)/u, "");
|
|
54
|
+
|
|
55
|
+
/** PURE. Does this tmux command line address the given server? */
|
|
56
|
+
export function commandTargetsServer(command: string, selector: TmuxServerSelector): boolean {
|
|
57
|
+
const words = command.split(/\s+/u);
|
|
58
|
+
for (let index = 0; index < words.length; index += 1) {
|
|
59
|
+
const word = words[index]!;
|
|
60
|
+
if (word === "-C") return false;
|
|
61
|
+
const flag = selector.kind === "path" ? "-S" : "-L";
|
|
62
|
+
const value = word === flag ? words[index + 1] : word.startsWith(flag) ? word.slice(2) : null;
|
|
63
|
+
if (value === null || value === undefined || value === "") continue;
|
|
64
|
+
return selector.kind === "path"
|
|
65
|
+
? withoutPrivate(value) === withoutPrivate(selector.path)
|
|
66
|
+
: value === selector.name;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** init, or a service manager that adopts orphans in its place. */
|
|
72
|
+
function adoptsOrphans(pid: number, command: string | null): boolean {
|
|
73
|
+
if (pid === 1) return true;
|
|
74
|
+
// The command's own name (its first word, with or without a path), never a
|
|
75
|
+
// word further on: a daemon run as `… --supervised launchd-user` is no manager.
|
|
76
|
+
return command !== null && /^(?:\S*\/)?(?:systemd|launchd|init)(?:\s|$)/u.test(command);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Ends tmux-ide control clients of this server whose daemon is gone. Never
|
|
81
|
+
* this daemon's own clients, another live daemon's, another server's, or any
|
|
82
|
+
* other program's. Returns the pids it ended. Failures end the pass; they
|
|
83
|
+
* never throw.
|
|
84
|
+
*/
|
|
85
|
+
export async function evictOrphanedControlClients(
|
|
86
|
+
io: OrphanedControlClientIo,
|
|
87
|
+
ownPid: number,
|
|
88
|
+
selector: TmuxServerSelector,
|
|
89
|
+
): Promise<number[]> {
|
|
90
|
+
let processes: readonly ProcessRow[];
|
|
91
|
+
try {
|
|
92
|
+
processes = await io.listProcesses();
|
|
93
|
+
} catch {
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
const byPid = new Map(processes.map((row) => [row.pid, row]));
|
|
97
|
+
const evicted: number[] = [];
|
|
98
|
+
for (const client of processes) {
|
|
99
|
+
if (
|
|
100
|
+
client.pid <= 1 ||
|
|
101
|
+
client.pid === ownPid ||
|
|
102
|
+
client.ppid === ownPid ||
|
|
103
|
+
!isTmuxIdeControlClientCommand(client.command) ||
|
|
104
|
+
!commandTargetsServer(client.command, selector) ||
|
|
105
|
+
!adoptsOrphans(client.ppid, byPid.get(client.ppid)?.command ?? null)
|
|
106
|
+
)
|
|
107
|
+
continue;
|
|
108
|
+
try {
|
|
109
|
+
io.kill(client.pid, "SIGTERM");
|
|
110
|
+
await io.delay(ORPHAN_KILL_GRACE_MS);
|
|
111
|
+
if (io.isAlive(client.pid)) io.kill(client.pid, "SIGKILL");
|
|
112
|
+
evicted.push(client.pid);
|
|
113
|
+
} catch {
|
|
114
|
+
// One client that cannot be signalled does not stop the pass.
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return evicted;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The real process table and signals. */
|
|
121
|
+
export function orphanedControlClientIo(): OrphanedControlClientIo {
|
|
122
|
+
return {
|
|
123
|
+
listProcesses: () =>
|
|
124
|
+
new Promise((resolve, reject) => {
|
|
125
|
+
execFile(
|
|
126
|
+
"/bin/ps",
|
|
127
|
+
["-xww", "-o", "pid=,ppid=,command="],
|
|
128
|
+
{
|
|
129
|
+
encoding: "utf8",
|
|
130
|
+
timeout: 3_000,
|
|
131
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
132
|
+
env: { LC_ALL: "C", PATH: "/usr/bin:/bin" },
|
|
133
|
+
},
|
|
134
|
+
(error, stdout) => (error ? reject(error) : resolve(parseProcessTable(stdout))),
|
|
135
|
+
);
|
|
136
|
+
}),
|
|
137
|
+
kill: (pid, signal) => {
|
|
138
|
+
process.kill(pid, signal);
|
|
139
|
+
},
|
|
140
|
+
isAlive: (pid) => {
|
|
141
|
+
try {
|
|
142
|
+
process.kill(pid, 0);
|
|
143
|
+
return true;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return (error as NodeJS.ErrnoException).code === "EPERM";
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
delay: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** What a catalog read found: an answer without sessions, or no answer. */
|
|
153
|
+
export type TmuxServerTrouble = "no-sessions" | "unanswered";
|
|
154
|
+
|
|
155
|
+
export const EMPTY_SERVER_PASS_INTERVAL_MS = 10_000;
|
|
156
|
+
export const EMPTY_SERVER_LOG_INTERVAL_MS = 60_000;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* One per owned server. Neither call is awaited by the owner, delays it or
|
|
160
|
+
* fails it.
|
|
161
|
+
* - `onOwnership`: one pass when the daemon takes the server.
|
|
162
|
+
* - `onTrouble`: a catalog read found the server with no sessions, or got no
|
|
163
|
+
* answer from a server whose socket is still there. A tmux server normally
|
|
164
|
+
* exits with its last session, so either it is configured to stay or it
|
|
165
|
+
* cannot exit. At most one pass per 10 s, and one warning per minute that
|
|
166
|
+
* names the state: scoped reads fail in it, and nothing else says why.
|
|
167
|
+
*/
|
|
168
|
+
export function createOrphanedControlClientWatch(options: {
|
|
169
|
+
readonly selector: TmuxServerSelector;
|
|
170
|
+
readonly serverId: string | null;
|
|
171
|
+
readonly io?: OrphanedControlClientIo;
|
|
172
|
+
readonly now?: () => number;
|
|
173
|
+
}): { onOwnership(): Promise<void>; onTrouble(kind: TmuxServerTrouble): Promise<void> } {
|
|
174
|
+
const io = options.io ?? orphanedControlClientIo();
|
|
175
|
+
const now = options.now ?? Date.now;
|
|
176
|
+
const fields = options.serverId ? { serverId: options.serverId } : {};
|
|
177
|
+
let lastPassAt = Number.NEGATIVE_INFINITY;
|
|
178
|
+
let lastLogAt = Number.NEGATIVE_INFINITY;
|
|
179
|
+
let running: Promise<number[]> | null = null;
|
|
180
|
+
const pass = (): Promise<number[]> => {
|
|
181
|
+
if (running) return running;
|
|
182
|
+
lastPassAt = now();
|
|
183
|
+
const current = evictOrphanedControlClients(io, process.pid, options.selector)
|
|
184
|
+
.catch((): number[] => [])
|
|
185
|
+
.then((pids) => {
|
|
186
|
+
if (pids.length > 0)
|
|
187
|
+
logger.warn("tmux-server-owners", "ended control clients a dead daemon left attached", {
|
|
188
|
+
...fields,
|
|
189
|
+
pids,
|
|
190
|
+
});
|
|
191
|
+
return pids;
|
|
192
|
+
})
|
|
193
|
+
.finally(() => {
|
|
194
|
+
if (running === current) running = null;
|
|
195
|
+
});
|
|
196
|
+
running = current;
|
|
197
|
+
return current;
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
onOwnership: () => pass().then(() => undefined),
|
|
201
|
+
async onTrouble(kind) {
|
|
202
|
+
if (now() - lastPassAt < EMPTY_SERVER_PASS_INTERVAL_MS) return;
|
|
203
|
+
const pids = await pass();
|
|
204
|
+
if (now() - lastLogAt < EMPTY_SERVER_LOG_INTERVAL_MS) return;
|
|
205
|
+
lastLogAt = now();
|
|
206
|
+
logger.warn(
|
|
207
|
+
"tmux-server-owners",
|
|
208
|
+
kind === "no-sessions"
|
|
209
|
+
? "tmux server answers with no sessions"
|
|
210
|
+
: "tmux server does not answer its catalog",
|
|
211
|
+
{ ...fields, endedControlClients: pids.length },
|
|
212
|
+
);
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
@@ -75,8 +75,8 @@ function nonEmpty(env: NodeJS.ProcessEnv, key: string): string | undefined {
|
|
|
75
75
|
return value ? value : undefined;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
function absolutePath(value: string, cwd: string, key: string): string {
|
|
79
|
-
const path = isAbsolute(value) ? value : resolve(cwd, value);
|
|
78
|
+
function absolutePath(value: string, cwd: () => string, key: string): string {
|
|
79
|
+
const path = isAbsolute(value) ? value : resolve(cwd(), value);
|
|
80
80
|
if (!isAbsolute(path)) throw new TypeError(`${key} must resolve to an absolute path`);
|
|
81
81
|
return path;
|
|
82
82
|
}
|
|
@@ -138,7 +138,18 @@ export function resolveRuntimeNamespace(
|
|
|
138
138
|
const within = (path: string, parent: string) => isInsideOrEqual(path, parent, identity);
|
|
139
139
|
const env = options.env ?? process.env;
|
|
140
140
|
const userHome = options.userHome ?? homedir();
|
|
141
|
-
|
|
141
|
+
// Read only for a relative setting (#585): `tmux-ide app` started from a
|
|
142
|
+
// deleted directory has no current directory, and needs none otherwise.
|
|
143
|
+
const cwd = (key: string) => (): string => {
|
|
144
|
+
if (options.cwd !== undefined) return options.cwd;
|
|
145
|
+
try {
|
|
146
|
+
return process.cwd();
|
|
147
|
+
} catch (error) {
|
|
148
|
+
throw new TypeError(`${key} is relative and the current directory no longer exists`, {
|
|
149
|
+
cause: error,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
};
|
|
142
153
|
const mode = runtimeMode(env);
|
|
143
154
|
const isolated = ISOLATED_MODES.has(mode);
|
|
144
155
|
const canonicalHome = join(userHome, ".tmux-ide");
|
|
@@ -184,10 +195,14 @@ export function resolveRuntimeNamespace(
|
|
|
184
195
|
throw new TypeError(`${mode} runtime requires an explicit ${STATE_HOME_ENV}`);
|
|
185
196
|
}
|
|
186
197
|
|
|
187
|
-
const stateHome = absolutePath(
|
|
198
|
+
const stateHome = absolutePath(
|
|
199
|
+
configuredHome ?? canonicalHome,
|
|
200
|
+
cwd(STATE_HOME_ENV),
|
|
201
|
+
STATE_HOME_ENV,
|
|
202
|
+
);
|
|
188
203
|
const registryDir = absolutePath(
|
|
189
204
|
nonEmpty(env, REGISTRY_DIR_ENV) ?? stateHome,
|
|
190
|
-
cwd,
|
|
205
|
+
cwd(REGISTRY_DIR_ENV),
|
|
191
206
|
REGISTRY_DIR_ENV,
|
|
192
207
|
);
|
|
193
208
|
const daemonInfoDir = absolutePath(
|
|
@@ -196,7 +211,7 @@ export function resolveRuntimeNamespace(
|
|
|
196
211
|
// This also keeps an explicitly empty DAEMON_INFO_DIR equivalent to
|
|
197
212
|
// "unset" instead of silently escaping a caller's isolated registry.
|
|
198
213
|
nonEmpty(env, DAEMON_INFO_DIR_ENV) ?? registryDir,
|
|
199
|
-
cwd,
|
|
214
|
+
cwd(DAEMON_INFO_DIR_ENV),
|
|
200
215
|
DAEMON_INFO_DIR_ENV,
|
|
201
216
|
);
|
|
202
217
|
const tmuxSocketName = nonEmpty(env, TMUX_SOCKET_NAME_ENV);
|
|
@@ -212,7 +227,10 @@ export function resolveRuntimeNamespace(
|
|
|
212
227
|
throw new TypeError(`${TMUX_SOCKET_NAME_ENV} is invalid`);
|
|
213
228
|
}
|
|
214
229
|
const tmuxSocket = tmuxSocketPath
|
|
215
|
-
? ({
|
|
230
|
+
? ({
|
|
231
|
+
kind: "path",
|
|
232
|
+
path: absolutePath(tmuxSocketPath, cwd(TMUX_SOCKET_PATH_ENV), TMUX_SOCKET_PATH_ENV),
|
|
233
|
+
} as const)
|
|
216
234
|
: ({ kind: "name", name: tmuxSocketName ?? "default" } as const);
|
|
217
235
|
if (isolated && tmuxSocket.kind === "name" && tmuxSocket.name === "default") {
|
|
218
236
|
throw new TypeError(`${mode} runtime requires a non-default ${TMUX_SOCKET_NAME_ENV}`);
|
|
@@ -151,11 +151,20 @@ export function resolveOrdinaryTmuxClient(
|
|
|
151
151
|
}
|
|
152
152
|
const candidates = configured
|
|
153
153
|
? [configured]
|
|
154
|
-
: (environment.PATH ?? "/usr/bin:/bin")
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
154
|
+
: (environment.PATH ?? "/usr/bin:/bin").split(delimiter).map((entry) => {
|
|
155
|
+
if (isAbsolute(entry)) return resolve(entry, "tmux");
|
|
156
|
+
// A relative or empty entry needs the current directory; a deleted
|
|
157
|
+
// one (#585) skips it instead of failing every tmux call.
|
|
158
|
+
try {
|
|
159
|
+
return resolve(
|
|
160
|
+
cwd instanceof URL ? fileURLToPath(cwd) : (cwd ?? process.cwd()),
|
|
161
|
+
entry,
|
|
162
|
+
"tmux",
|
|
163
|
+
);
|
|
164
|
+
} catch {
|
|
165
|
+
return "";
|
|
166
|
+
}
|
|
167
|
+
});
|
|
159
168
|
let executable: string | undefined;
|
|
160
169
|
for (const candidate of candidates) {
|
|
161
170
|
try {
|
|
@@ -77,15 +77,26 @@ export const DEFAULT_HOOK_HEALTHCHECK_SCHEDULE: HookHealthcheckSchedule = Object
|
|
|
77
77
|
|
|
78
78
|
export type HookHealthcheckOutcome = "healthy" | "repaired" | "failed";
|
|
79
79
|
|
|
80
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Pure cadence rule: healthy checks double the wait up to the cap; a repair or
|
|
82
|
+
* failure resets it to the base. A check that is unhealthy again right after
|
|
83
|
+
* one that was (`consecutiveUnhealthy` of 2 or more) backs off as well: on a
|
|
84
|
+
* tmux server that is shutting down but cannot exit, every repair "succeeds"
|
|
85
|
+
* and is gone at the next check, and the base cadence then cost about 400 tmux
|
|
86
|
+
* calls a minute for as long as the server lingered.
|
|
87
|
+
*/
|
|
81
88
|
export function nextHookHealthcheckDelay(
|
|
82
89
|
previousMs: number,
|
|
83
90
|
outcome: HookHealthcheckOutcome,
|
|
84
91
|
schedule: HookHealthcheckSchedule = DEFAULT_HOOK_HEALTHCHECK_SCHEDULE,
|
|
92
|
+
consecutiveUnhealthy = 1,
|
|
85
93
|
): number {
|
|
86
94
|
const base = Math.max(1, Math.floor(schedule.baseMs));
|
|
87
95
|
const max = Math.max(base, Math.floor(schedule.maxMs));
|
|
88
|
-
if (outcome !== "healthy")
|
|
96
|
+
if (outcome !== "healthy") {
|
|
97
|
+
const streak = Math.max(1, Math.min(32, Math.floor(consecutiveUnhealthy) || 1));
|
|
98
|
+
return Math.min(base * 2 ** (streak - 1), max);
|
|
99
|
+
}
|
|
89
100
|
const previous = Number.isFinite(previousMs) ? Math.max(base, Math.floor(previousMs)) : base;
|
|
90
101
|
return Math.min(previous * 2, max);
|
|
91
102
|
}
|
|
@@ -371,6 +382,8 @@ export class TmuxExternalInteractionObserver {
|
|
|
371
382
|
readonly #healthcheckSchedule: HookHealthcheckSchedule;
|
|
372
383
|
#healthcheckDelayMs: number;
|
|
373
384
|
#lastHealthcheckOutcome: HookHealthcheckOutcome = "failed";
|
|
385
|
+
/** Scheduled checks in a row that did not find the hooks intact. */
|
|
386
|
+
#unhealthyChecks = 0;
|
|
374
387
|
readonly #onAvailability: ((available: boolean) => void) | undefined;
|
|
375
388
|
readonly #onGap: ((gap: ExternalTmuxInteractionGap) => void) | undefined;
|
|
376
389
|
#tmuxWork: Promise<unknown> = Promise.resolve();
|
|
@@ -520,12 +533,14 @@ export class TmuxExternalInteractionObserver {
|
|
|
520
533
|
if (!this.#abort.signal.aborted) this.#reportGap("hook-repair-failed");
|
|
521
534
|
outcome = "failed";
|
|
522
535
|
}
|
|
536
|
+
this.#unhealthyChecks = outcome === "healthy" ? 0 : this.#unhealthyChecks + 1;
|
|
523
537
|
// A signal-path reset that landed during this check wins over the outcome.
|
|
524
538
|
if (this.#healthcheckDelayMs === delayBefore) {
|
|
525
539
|
this.#healthcheckDelayMs = nextHookHealthcheckDelay(
|
|
526
540
|
delayBefore,
|
|
527
541
|
outcome,
|
|
528
542
|
this.#healthcheckSchedule,
|
|
543
|
+
this.#unhealthyChecks,
|
|
529
544
|
);
|
|
530
545
|
}
|
|
531
546
|
if (this.#hookHealthcheck === null) this.#scheduleHealthcheck();
|
|
@@ -647,11 +662,20 @@ export class TmuxExternalInteractionObserver {
|
|
|
647
662
|
const work = this.#serializeTmux(async () => {
|
|
648
663
|
const finish = this.#beginDiagnostic("healthcheck");
|
|
649
664
|
try {
|
|
650
|
-
|
|
665
|
+
const hooks = await this.#ownedHooksPresent(this.#abort.signal);
|
|
666
|
+
if (hooks === "present") {
|
|
651
667
|
this.#lastHealthcheckOutcome = "healthy";
|
|
652
668
|
finish(true);
|
|
653
669
|
return;
|
|
654
670
|
}
|
|
671
|
+
if (hooks === "unknown") {
|
|
672
|
+
// The check could not be made (tmux timed out or could not be
|
|
673
|
+
// started). That says nothing about the hooks: leave them, report
|
|
674
|
+
// no gap, and ask again at the base cadence.
|
|
675
|
+
this.#lastHealthcheckOutcome = "failed";
|
|
676
|
+
finish(false);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
655
679
|
// Hooks that were installed and are now gone dropped every interaction
|
|
656
680
|
// since their removal; the repair restores future observation only.
|
|
657
681
|
if (this.#installed) this.#reportGap("hooks-replaced");
|
|
@@ -917,7 +941,7 @@ export class TmuxExternalInteractionObserver {
|
|
|
917
941
|
this.#setInstalled(false);
|
|
918
942
|
}
|
|
919
943
|
|
|
920
|
-
async #ownedHooksPresent(signal?: AbortSignal): Promise<
|
|
944
|
+
async #ownedHooksPresent(signal?: AbortSignal): Promise<"present" | "missing" | "unknown"> {
|
|
921
945
|
// One client verifies both hook arrays: tmux runs the `;`-separated list in
|
|
922
946
|
// a single command queue and prints each array's lines in order.
|
|
923
947
|
let output: string;
|
|
@@ -927,12 +951,12 @@ export class TmuxExternalInteractionObserver {
|
|
|
927
951
|
signal,
|
|
928
952
|
);
|
|
929
953
|
} catch {
|
|
930
|
-
return
|
|
954
|
+
return "unknown";
|
|
931
955
|
}
|
|
932
|
-
return (
|
|
933
|
-
ownedHookInstalled(output, "after-send-keys", this.#bufferName) &&
|
|
956
|
+
return ownedHookInstalled(output, "after-send-keys", this.#bufferName) &&
|
|
934
957
|
ownedHookInstalled(output, "after-capture-pane", this.#bufferName)
|
|
935
|
-
|
|
958
|
+
? "present"
|
|
959
|
+
: "missing";
|
|
936
960
|
}
|
|
937
961
|
|
|
938
962
|
get available(): boolean {
|
|
@@ -54,6 +54,7 @@ import { liveSessionIdForNativeIdentity } from "../terminal/protocol/live-sessio
|
|
|
54
54
|
import { SessionRuntimeRegistry } from "../terminal/session-runtime/registry.ts";
|
|
55
55
|
import { createSessionRuntimeMultiplexerBackend } from "../terminal/session-runtime/multiplexer-backend.ts";
|
|
56
56
|
import { TmuxExternalInteractionObserver } from "./tmux-external-interaction-observer.ts";
|
|
57
|
+
import { createOrphanedControlClientWatch } from "./orphaned-control-clients.ts";
|
|
57
58
|
import { InteractionReceiptJournal } from "./interaction-receipt-journal.ts";
|
|
58
59
|
import { InteractionObservationStatusStore } from "./interaction-observation-status.ts";
|
|
59
60
|
import { InteractionEvidenceAuthority } from "./interaction-evidence-authority.ts";
|
|
@@ -118,9 +119,18 @@ export function createNativeTmuxServerCatalog(
|
|
|
118
119
|
workspaceRegistry: WorkspaceRegistry,
|
|
119
120
|
run: (args: readonly string[], signal?: AbortSignal) => Promise<string>,
|
|
120
121
|
assertOpen: () => void = () => undefined,
|
|
122
|
+
/** Told when the server has no sessions or does not answer; never awaited. */
|
|
123
|
+
onTrouble?: (kind: "no-sessions" | "unanswered") => unknown,
|
|
121
124
|
): () => Promise<NativeLiveSessionSummary[]> {
|
|
122
125
|
return async (): Promise<NativeLiveSessionSummary[]> => {
|
|
123
126
|
assertOpen();
|
|
127
|
+
const tell = (kind: "no-sessions" | "unanswered"): void => {
|
|
128
|
+
try {
|
|
129
|
+
void Promise.resolve(onTrouble?.(kind)).catch(() => undefined);
|
|
130
|
+
} catch {
|
|
131
|
+
/* A watcher never decides the catalog. */
|
|
132
|
+
}
|
|
133
|
+
};
|
|
124
134
|
const raw = await run(
|
|
125
135
|
[
|
|
126
136
|
"-N",
|
|
@@ -139,7 +149,10 @@ export function createNativeTmuxServerCatalog(
|
|
|
139
149
|
.join(" "),
|
|
140
150
|
],
|
|
141
151
|
AbortSignal.timeout(2_000),
|
|
142
|
-
)
|
|
152
|
+
).catch((error: unknown) => {
|
|
153
|
+
tell("unanswered");
|
|
154
|
+
throw error;
|
|
155
|
+
});
|
|
143
156
|
assertOpen();
|
|
144
157
|
const rows = new Map<
|
|
145
158
|
string,
|
|
@@ -153,7 +166,9 @@ export function createNativeTmuxServerCatalog(
|
|
|
153
166
|
>();
|
|
154
167
|
// Only the successful server-side zero-session branch proves emptiness.
|
|
155
168
|
// Command errors, unsupported formats and unwitnessed blank output stay failures.
|
|
156
|
-
const
|
|
169
|
+
const empty = raw.trim() === "tmux-ide-empty-catalog-v1";
|
|
170
|
+
if (empty) tell("no-sessions");
|
|
171
|
+
const lines = empty ? [] : raw.trim().split("\n");
|
|
157
172
|
for (const line of lines) {
|
|
158
173
|
const [pid, id, created, name, pane, window, activity, ...pathParts] = line.split("\t");
|
|
159
174
|
const dir = pathParts.join("\t");
|
|
@@ -241,6 +256,11 @@ export async function createNativeTmuxServerOwner(options: NativeTmuxServerOwner
|
|
|
241
256
|
authority,
|
|
242
257
|
nativeServerIdentity,
|
|
243
258
|
);
|
|
259
|
+
const orphanWatch = createOrphanedControlClientWatch({
|
|
260
|
+
selector: authority.socketSelector,
|
|
261
|
+
serverId: options.serverId,
|
|
262
|
+
});
|
|
263
|
+
void orphanWatch.onOwnership();
|
|
244
264
|
const multiplexer = new WorkspaceMultiplexerAuthority({
|
|
245
265
|
daemonInstanceId: generation,
|
|
246
266
|
registry: workspaceRegistry,
|
|
@@ -540,7 +560,12 @@ export async function createNativeTmuxServerOwner(options: NativeTmuxServerOwner
|
|
|
540
560
|
registry: sessionRuntimeRegistry,
|
|
541
561
|
resolveSession: (name) => workspaceRegistry.get(name)?.sessionName ?? null,
|
|
542
562
|
});
|
|
543
|
-
const catalog = createNativeTmuxServerCatalog(
|
|
563
|
+
const catalog = createNativeTmuxServerCatalog(
|
|
564
|
+
workspaceRegistry,
|
|
565
|
+
generationRunAsync,
|
|
566
|
+
assertOpen,
|
|
567
|
+
orphanWatch.onTrouble,
|
|
568
|
+
);
|
|
544
569
|
const sessionCreator = createNativeTmuxSessionCreator({
|
|
545
570
|
generation,
|
|
546
571
|
registry: workspaceRegistry,
|
|
@@ -237,6 +237,40 @@ export function getUpdateStatus({
|
|
|
237
237
|
return deriveStatus(cache?.latest ?? null, currentVersion);
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
+
/** Where a status came from: the registry just now, or the last answer on disk. */
|
|
241
|
+
export type UpdateStatusSource = "registry" | "cache";
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* io — the answer for a user who asked (`tmux-ide update`): fetch the registry
|
|
245
|
+
* now, with a short timeout, and store what it said. The cache alone can be
|
|
246
|
+
* weeks old on a machine that rarely runs the updater tick. Falls back to the
|
|
247
|
+
* cache when the registry cannot be reached, and says so in `source`.
|
|
248
|
+
*/
|
|
249
|
+
export async function refreshUpdateStatus({
|
|
250
|
+
currentVersion = getCurrentVersion(),
|
|
251
|
+
now = Date.now(),
|
|
252
|
+
timeoutMs = 3000,
|
|
253
|
+
fetchLatest = fetchLatestVersion,
|
|
254
|
+
}: UpdateScope & {
|
|
255
|
+
now?: number;
|
|
256
|
+
timeoutMs?: number;
|
|
257
|
+
fetchLatest?: typeof fetchLatestVersion;
|
|
258
|
+
} = {}): Promise<UpdateStatus & { source: UpdateStatusSource }> {
|
|
259
|
+
const fetched = await fetchLatest(timeoutMs, updateChannel(currentVersion));
|
|
260
|
+
const cache = readUpdateCache({ currentVersion });
|
|
261
|
+
if (fetched === null)
|
|
262
|
+
return { ...deriveStatus(cache?.latest ?? null, currentVersion), source: "cache" };
|
|
263
|
+
writeUpdateCache(
|
|
264
|
+
{
|
|
265
|
+
lastCheckedAt: now,
|
|
266
|
+
latest: fetched,
|
|
267
|
+
...(cache?.notified ? { notified: cache.notified } : {}),
|
|
268
|
+
},
|
|
269
|
+
{ currentVersion },
|
|
270
|
+
);
|
|
271
|
+
return { ...deriveStatus(fetched, currentVersion), source: "registry" };
|
|
272
|
+
}
|
|
273
|
+
|
|
240
274
|
/**
|
|
241
275
|
* io — the throttled network refresh (called fire-and-forget from the updater
|
|
242
276
|
* tick). Does nothing if the last check was inside {@link CHECK_INTERVAL_MS};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { compareProductVersions } from "./semver.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What `tmux-ide update --daemon` tells the user once a daemon is running.
|
|
5
|
+
*
|
|
6
|
+
* A CLI never replaces a NEWER daemon (canonical-daemon-bootstrap's
|
|
7
|
+
* needsReplacement): clients of the newer version may depend on it. That is
|
|
8
|
+
* by design. But after a rollback (a newer daemon, an older CLI) the command
|
|
9
|
+
* used to report "Daemon ready" for the newer daemon and nothing else, so it
|
|
10
|
+
* looked as if it had done nothing. It now says the daemon was left running
|
|
11
|
+
* and how to run this version's daemon instead.
|
|
12
|
+
*/
|
|
13
|
+
export function daemonUpdateOutcome(
|
|
14
|
+
running: {
|
|
15
|
+
readonly productVersion: string;
|
|
16
|
+
readonly pid: number;
|
|
17
|
+
readonly supervised: boolean;
|
|
18
|
+
/** The daemon runs inside another tmux-ide process (an app or TUI), not on its own. */
|
|
19
|
+
readonly embedded?: boolean;
|
|
20
|
+
},
|
|
21
|
+
cliVersion: string,
|
|
22
|
+
): { status: "ready" | "kept-newer"; message: string } {
|
|
23
|
+
if (compareProductVersions(running.productVersion, cliVersion) !== 1)
|
|
24
|
+
return { status: "ready", message: `Daemon ready: ${running.productVersion}` };
|
|
25
|
+
// A service restart runs the launcher the service was set up with, which need
|
|
26
|
+
// not be this CLI's installation: say what it does, not which version it loads.
|
|
27
|
+
const how = running.supervised
|
|
28
|
+
? "restart its service with tmux-ide daemon service restart; the service starts " +
|
|
29
|
+
`the installation it was set up with, so if that is not v${cliVersion}, set the ` +
|
|
30
|
+
"service up from this installation first"
|
|
31
|
+
: running.embedded
|
|
32
|
+
? `quit what it runs in: it runs inside another tmux-ide process (pid ${running.pid}), ` +
|
|
33
|
+
"such as an open app, then run tmux-ide update --daemon again"
|
|
34
|
+
: `stop it (pid ${running.pid}), then run tmux-ide update --daemon again`;
|
|
35
|
+
return {
|
|
36
|
+
status: "kept-newer",
|
|
37
|
+
message:
|
|
38
|
+
`The running daemon is v${running.productVersion}, newer than this CLI (v${cliVersion}). ` +
|
|
39
|
+
`It was left running: a CLI never replaces a newer daemon. ` +
|
|
40
|
+
`To run v${cliVersion}'s daemon instead, ${how}.`,
|
|
41
|
+
};
|
|
42
|
+
}
|