pi-squire 0.13.1 → 0.15.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.
- package/CHANGELOG.md +141 -1
- package/README.md +192 -87
- package/package.json +3 -3
- package/src/agents.ts +361 -111
- package/src/child.ts +72 -21
- package/src/index.ts +243 -67
- package/src/navigator.ts +28 -52
- package/src/new-delegate.ts +17 -8
- package/src/poll.ts +10 -33
- package/src/result-sessions.ts +39 -0
- package/src/results.ts +2 -0
- package/src/resume-checks.ts +45 -12
- package/src/resume.ts +7 -2
- package/src/tmux.ts +4 -88
- package/src/tracking.ts +1 -5
- package/src/popups.ts +0 -49
package/src/tmux.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The tmux operations `delegate` needs, behind an interface so tests can use a
|
|
2
2
|
// fake. This client talks to tmux with `execFile` and no shell.
|
|
3
|
-
import { execFile
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
4
|
|
|
5
5
|
import type { WindowPlacement } from "./window-placement.ts";
|
|
6
6
|
|
|
@@ -18,7 +18,7 @@ export type TmuxWindow = { id: string; session: string; name: string; exited: bo
|
|
|
18
18
|
/** A window just opened: its id and the name of the session it is in, when known. */
|
|
19
19
|
export type OpenedWindow = { id: string; session: string | undefined };
|
|
20
20
|
|
|
21
|
-
/** The oldest tmux delegate windows need: `new-session -e`
|
|
21
|
+
/** The oldest tmux delegate windows need: `new-session -e` arrived in 3.2. */
|
|
22
22
|
export const MIN_TMUX = { major: 3, minor: 2 } as const;
|
|
23
23
|
|
|
24
24
|
export type TmuxVersion = { text: string; major: number; minor: number };
|
|
@@ -62,9 +62,6 @@ export class WindowOpenError extends Error {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/** A window as the popup needs it: its id and its name, which titles the popup. */
|
|
66
|
-
export type PopupWindow = { id: string; name: string };
|
|
67
|
-
|
|
68
65
|
/** The session the parent's pane is in: its id (`$N`) and name. */
|
|
69
66
|
export type OwnSession = { id: string; name: string };
|
|
70
67
|
|
|
@@ -89,16 +86,6 @@ export interface TmuxClient {
|
|
|
89
86
|
clientFor(session: string): Promise<string | undefined>;
|
|
90
87
|
/** Show the window to a client: `switch-client -c <client> -t @N`. */
|
|
91
88
|
switchTo(client: string, windowId: string): Promise<void>;
|
|
92
|
-
/**
|
|
93
|
-
* Open a popup over a client that attaches to the window's session at the
|
|
94
|
-
* window. `display-popup` returns only when the popup closes, so this
|
|
95
|
-
* resolves once the popup was launched, not when it ends, and a launch that
|
|
96
|
-
* fails at once rejects. `onExit` runs when the popup's process ends, however
|
|
97
|
-
* the popup was closed, and not when the launch failed.
|
|
98
|
-
*/
|
|
99
|
-
popup(client: string, window: PopupWindow, onExit: () => void): Promise<void>;
|
|
100
|
-
/** Close the popup on a client (`display-popup -C`). */
|
|
101
|
-
closePopup(client: string): Promise<void>;
|
|
102
89
|
/** The window's visible pane as text (`capture-pane -p`). */
|
|
103
90
|
capture(windowId: string): Promise<string>;
|
|
104
91
|
}
|
|
@@ -139,61 +126,6 @@ async function parentSession(run: Run): Promise<Parent | undefined> {
|
|
|
139
126
|
return id.trim() === "" ? undefined : { id: id.trim(), name: name.join("\t"), companion: marker === "1" };
|
|
140
127
|
}
|
|
141
128
|
|
|
142
|
-
/**
|
|
143
|
-
* The key that starts a tmux command, as tmux names it (`C-b`), for the
|
|
144
|
-
* popup's title; `prefix` when it cannot be read or is unset.
|
|
145
|
-
*/
|
|
146
|
-
async function prefixKey(run: Run): Promise<string> {
|
|
147
|
-
try {
|
|
148
|
-
const key = (await run(["show-options", "-gv", "prefix"])).trim();
|
|
149
|
-
return key === "" || key === "None" ? "prefix" : key;
|
|
150
|
-
} catch {
|
|
151
|
-
return "prefix";
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** How long a launched popup may take to fail before it counts as shown. */
|
|
156
|
-
const POPUP_SETTLE_MS = 300;
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Launch `display-popup`, which blocks until the popup closes, detached and
|
|
160
|
-
* outside `runTmux`'s timeout. Resolves once it is up; rejects on a spawn
|
|
161
|
-
* error or an exit within the settle time. Its stderr is read only until then
|
|
162
|
-
* and then released, so the open popup keeps nothing of this process alive;
|
|
163
|
-
* `onExit` runs when the popup's process ends, however it was closed.
|
|
164
|
-
*/
|
|
165
|
-
function launchPopup(args: string[], onExit: () => void): Promise<void> {
|
|
166
|
-
return new Promise((resolve, reject) => {
|
|
167
|
-
let settled = false;
|
|
168
|
-
let stderr = "";
|
|
169
|
-
const child = spawn("tmux", args, { detached: true, stdio: ["ignore", "ignore", "pipe"] });
|
|
170
|
-
const onData = (chunk: Buffer): void => {
|
|
171
|
-
if (stderr.length < 4096) stderr += chunk.toString();
|
|
172
|
-
};
|
|
173
|
-
child.stderr?.on("data", onData);
|
|
174
|
-
const settle = (error?: Error): void => {
|
|
175
|
-
if (settled) return;
|
|
176
|
-
settled = true;
|
|
177
|
-
clearTimeout(timer);
|
|
178
|
-
child.stderr?.off("data", onData);
|
|
179
|
-
child.stderr?.destroy();
|
|
180
|
-
if (error === undefined) resolve();
|
|
181
|
-
else reject(error);
|
|
182
|
-
};
|
|
183
|
-
const timer = setTimeout(() => settle(), POPUP_SETTLE_MS);
|
|
184
|
-
child.on("error", (err) => settle(err));
|
|
185
|
-
child.on("close", (code) => {
|
|
186
|
-
if (!settled && code !== 0) {
|
|
187
|
-
settle(new Error(stderr.trim() !== "" ? stderr.trim() : `tmux display-popup exited with ${code}`));
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
settle();
|
|
191
|
-
onExit();
|
|
192
|
-
});
|
|
193
|
-
child.unref();
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
129
|
const envArgs = (spec: WindowSpec): string[] =>
|
|
198
130
|
Object.entries(spec.env).flatMap(([key, value]) => ["-e", `${key}=${value}`]);
|
|
199
131
|
|
|
@@ -263,10 +195,8 @@ function openCurrent(run: Run, spec: WindowSpec, parent: Parent | undefined): Pr
|
|
|
263
195
|
return create(run, args);
|
|
264
196
|
}
|
|
265
197
|
|
|
266
|
-
/** The methods that reach a delegate's window from the user's terminal: clients, switch,
|
|
267
|
-
function navigation(
|
|
268
|
-
run: Run,
|
|
269
|
-
): Pick<TmuxClient, "ownSession" | "clientFor" | "switchTo" | "popup" | "closePopup" | "capture"> {
|
|
198
|
+
/** The methods that reach a delegate's window from the user's terminal: clients, switch, capture. */
|
|
199
|
+
function navigation(run: Run): Pick<TmuxClient, "ownSession" | "clientFor" | "switchTo" | "capture"> {
|
|
270
200
|
return {
|
|
271
201
|
async ownSession() {
|
|
272
202
|
const parent = await parentSession(run);
|
|
@@ -289,20 +219,6 @@ function navigation(
|
|
|
289
219
|
await run(["switch-client", "-c", client, "-t", windowId]);
|
|
290
220
|
},
|
|
291
221
|
|
|
292
|
-
async popup(client, window, onExit) {
|
|
293
|
-
if (!/^@[0-9]+$/.test(window.id)) throw new Error(`${window.id} is not a tmux window id`);
|
|
294
|
-
const sessionId = (await run(["display-message", "-p", "-t", window.id, "-F", "#{session_id}"])).trim();
|
|
295
|
-
if (!/^\$[0-9]+$/.test(sessionId)) throw new Error(`tmux reported an invalid session id for ${window.id}`);
|
|
296
|
-
// Ids are `@N` and `$N` only; quoted so the popup's shell does not expand `$N`.
|
|
297
|
-
const command = `tmux select-window -t '${window.id}' \\; attach-session -t '${sessionId}'`;
|
|
298
|
-
const title = `${window.name} · ${await prefixKey(run)} d to close`;
|
|
299
|
-
await launchPopup(["display-popup", "-c", client, "-E", "-w", "90%", "-h", "90%", "-T", title, command], onExit);
|
|
300
|
-
},
|
|
301
|
-
|
|
302
|
-
async closePopup(client) {
|
|
303
|
-
await run(["display-popup", "-C", "-c", client]);
|
|
304
|
-
},
|
|
305
|
-
|
|
306
222
|
async capture(windowId) {
|
|
307
223
|
return run(["capture-pane", "-p", "-t", windowId]);
|
|
308
224
|
},
|
package/src/tracking.ts
CHANGED
|
@@ -9,7 +9,6 @@ import { type Static, Type } from "typebox";
|
|
|
9
9
|
|
|
10
10
|
import { type Mark, readMarks } from "./limits.ts";
|
|
11
11
|
import { createPoll } from "./poll.ts";
|
|
12
|
-
import type { Popup } from "./popups.ts";
|
|
13
12
|
import { attemptLines, cwdLine, modelLabel, snapshotLines, workspaceLine } from "./render.ts";
|
|
14
13
|
import {
|
|
15
14
|
type DelegationState,
|
|
@@ -37,10 +36,8 @@ export { listWindows, ownWindow, windowState };
|
|
|
37
36
|
export type Tracking = {
|
|
38
37
|
/** Register `delegation_status` and `delegation_close`. */
|
|
39
38
|
registerTools(): void;
|
|
40
|
-
/** Start the poll when
|
|
39
|
+
/** Start the poll when a delegation is running. */
|
|
41
40
|
startPolling(): void;
|
|
42
|
-
/** Remember a popup the parent opened; the poll closes it once its window is gone. Returns the call that forgets it when its process ends. */
|
|
43
|
-
trackPopup(popup: Popup): () => void;
|
|
44
41
|
/** Close one delegation as `delegation_close` does; throws as the tool does. */
|
|
45
42
|
close(id: string): Promise<CloseResult>;
|
|
46
43
|
/** On `session_start`: rebuild the records and restart the poll. */
|
|
@@ -410,7 +407,6 @@ export function createTracking(
|
|
|
410
407
|
pi.registerTool(closeTool(results, tmux) as never);
|
|
411
408
|
},
|
|
412
409
|
startPolling: poll.start,
|
|
413
|
-
trackPopup: poll.track,
|
|
414
410
|
close: (id: string): Promise<CloseResult> => closeDelegation(results, tmux, id),
|
|
415
411
|
restore(ctx: ExtensionContext): void {
|
|
416
412
|
poll.arm();
|
package/src/popups.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
// The popups the parent opened over the user's tmux clients. tmux closes a
|
|
2
|
-
// popup's contents when the delegate's window goes, but not the popup itself:
|
|
3
|
-
// the poll closes it, through this registry, once its window is gone. A client
|
|
4
|
-
// shows one popup at a time, so the registry holds the current popup per
|
|
5
|
-
// client; a new popup replaces the old record, and a record is dropped when
|
|
6
|
-
// its own popup's process ends, so a stale record never closes a newer popup.
|
|
7
|
-
import type { TmuxClient, TmuxWindow } from "./tmux.ts";
|
|
8
|
-
|
|
9
|
-
/** A popup the parent opened: the window it shows and the client it is on. */
|
|
10
|
-
export type Popup = { windowId: string; windowName: string; client: string };
|
|
11
|
-
|
|
12
|
-
export type Popups = {
|
|
13
|
-
/** Remember a popup as its client's current one; returns the call that forgets exactly this popup. */
|
|
14
|
-
track(popup: Popup): () => void;
|
|
15
|
-
/** How many popups are remembered. */
|
|
16
|
-
count(): number;
|
|
17
|
-
/** Forget every popup, closing none. */
|
|
18
|
-
clear(): void;
|
|
19
|
-
/** Close each current popup whose window is not in `windows`; a failed listing (undefined) keeps them all for the next tick. */
|
|
20
|
-
closeGone(windows: TmuxWindow[] | undefined): Promise<void>;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export function createPopups(tmux: TmuxClient): Popups {
|
|
24
|
-
const current = new Map<string, Popup>();
|
|
25
|
-
return {
|
|
26
|
-
track(popup: Popup): () => void {
|
|
27
|
-
const entry = { ...popup };
|
|
28
|
-
current.set(entry.client, entry);
|
|
29
|
-
return (): void => {
|
|
30
|
-
if (current.get(entry.client) === entry) current.delete(entry.client);
|
|
31
|
-
};
|
|
32
|
-
},
|
|
33
|
-
count: (): number => current.size,
|
|
34
|
-
clear: (): void => current.clear(),
|
|
35
|
-
async closeGone(windows: TmuxWindow[] | undefined): Promise<void> {
|
|
36
|
-
if (windows === undefined) return;
|
|
37
|
-
for (const entry of [...current.values()]) {
|
|
38
|
-
const here = windows.some((window) => window.id === entry.windowId && window.name === entry.windowName);
|
|
39
|
-
if (here || current.get(entry.client) !== entry) continue;
|
|
40
|
-
current.delete(entry.client); // forgotten first, so nothing closes it twice
|
|
41
|
-
try {
|
|
42
|
-
await tmux.closePopup(entry.client);
|
|
43
|
-
} catch {
|
|
44
|
-
// The popup may already be closed or its client gone; one try, then forgotten.
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
}
|