tmux-ide 2.9.20 → 2.9.22
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 +16998 -4283
- package/package.json +3 -2
- package/packages/contracts/src/command-catalog.ts +116 -0
- package/packages/contracts/src/commands.ts +5 -4
- package/packages/contracts/src/daemon-events.ts +2 -1
- package/packages/contracts/src/index.ts +3 -0
- package/packages/contracts/src/pane-reader.ts +121 -0
- package/packages/contracts/src/pane-stream.ts +18 -0
- package/packages/contracts/src/plain-text.ts +61 -0
- package/packages/contracts/src/visual-theme-presets.ts +58 -2
- package/packages/daemon/dist/command-center/command-catalog-routes.js +78 -0
- package/packages/daemon/dist/command-center/plugin-routes.js +84 -0
- package/packages/daemon/dist/command-center/server.js +13 -0
- package/packages/daemon/dist/command-center/tmux-server-reader.js +41 -0
- package/packages/daemon/dist/command-center/tmux-servers.js +6 -0
- package/packages/daemon/dist/doctor.js +106 -43
- package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
- package/packages/daemon/dist/lib/cli-command.js +13 -0
- package/packages/daemon/dist/lib/daemon-embed.js +39 -1
- package/packages/daemon/dist/lib/headless-daemon.js +1 -0
- package/packages/daemon/dist/lib/pane-reader-files.js +226 -0
- package/packages/daemon/dist/lib/remote-tmux-command.js +8 -5
- package/packages/daemon/dist/lib/tmux-client-execution.js +73 -7
- package/packages/daemon/dist/lib/tmux-pane-reader.js +105 -0
- package/packages/daemon/dist/lib/tmux-server-features.js +38 -0
- package/packages/daemon/dist/lib/tmux-server-owner.js +9 -0
- package/packages/daemon/dist/lib/worktree.js +18 -0
- 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/open-browser.js +26 -12
- package/packages/daemon/dist/open-reader.js +84 -0
- package/packages/daemon/dist/plugins/action-catalog.js +129 -0
- package/packages/daemon/dist/plugins/action-cli.js +212 -0
- package/packages/daemon/dist/plugins/api-table.js +303 -0
- package/packages/daemon/dist/plugins/cli.js +556 -0
- package/packages/daemon/dist/plugins/consent.js +107 -0
- package/packages/daemon/dist/plugins/daemon-plugins.js +124 -0
- package/packages/daemon/dist/plugins/discovery.js +142 -0
- package/packages/daemon/dist/plugins/examples/worktree-tasks.js +115 -0
- package/packages/daemon/dist/plugins/exec-sandbox.js +202 -0
- package/packages/daemon/dist/plugins/host/actions-prelude.js +279 -0
- package/packages/daemon/dist/plugins/host/entry.js +4 -0
- package/packages/daemon/dist/plugins/host/host-api-prelude.js +232 -0
- package/packages/daemon/dist/plugins/host/lua-state.js +493 -0
- package/packages/daemon/dist/plugins/host/main.js +322 -0
- package/packages/daemon/dist/plugins/host/sandbox.js +299 -0
- package/packages/daemon/dist/plugins/host-calls.js +279 -0
- package/packages/daemon/dist/plugins/manifest.js +69 -0
- package/packages/daemon/dist/plugins/plugin-exec.js +261 -0
- package/packages/daemon/dist/plugins/plugin-log.js +54 -0
- package/packages/daemon/dist/plugins/plugin-store.js +74 -0
- package/packages/daemon/dist/plugins/protocol.js +252 -0
- package/packages/daemon/dist/plugins/safe-files.js +97 -0
- package/packages/daemon/dist/plugins/supervisor.js +790 -0
- package/packages/daemon/dist/plugins/worktree-calls.js +154 -0
- package/packages/daemon/dist/restore.js +57 -40
- package/packages/daemon/dist/terminal/mirror/session-channel.js +4 -1
- package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +12 -3
- package/packages/daemon/dist/terminal/protocol/reader-option.js +66 -0
- package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +7 -2
- package/packages/daemon/dist/tmux-upgrade/cli.js +106 -0
- package/packages/daemon/dist/tmux-upgrade/io.js +540 -0
- package/packages/daemon/dist/tmux-upgrade/plan.js +238 -0
- package/packages/daemon/dist/tmux-upgrade/run.js +199 -0
- package/packages/daemon/dist/tui/chrome/snapshot.js +3 -4
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-navigation.js +64 -4
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +62 -5
- package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-agent-navigation.js +12 -8
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +30 -2
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-commands.js +152 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-form.js +251 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-interaction-controller.js +125 -111
- package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +14 -0
- package/packages/daemon/dist/tui/mirror/workspace/palette-list-layout.js +5 -0
- package/packages/daemon/src/command-center/command-catalog-routes.ts +116 -0
- package/packages/daemon/src/command-center/plugin-routes.ts +110 -0
- package/packages/daemon/src/command-center/server.ts +15 -0
- package/packages/daemon/src/command-center/tmux-server-reader.ts +62 -0
- package/packages/daemon/src/command-center/tmux-servers.ts +6 -0
- package/packages/daemon/src/doctor.ts +118 -45
- package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
- package/packages/daemon/src/lib/cli-command.ts +16 -0
- package/packages/daemon/src/lib/daemon-embed.ts +59 -0
- package/packages/daemon/src/lib/headless-daemon.ts +1 -0
- package/packages/daemon/src/lib/pane-reader-files.ts +294 -0
- package/packages/daemon/src/lib/remote-tmux-command.ts +13 -6
- package/packages/daemon/src/lib/tmux-client-execution.ts +79 -7
- package/packages/daemon/src/lib/tmux-pane-reader.ts +144 -0
- package/packages/daemon/src/lib/tmux-server-features.ts +43 -0
- package/packages/daemon/src/lib/tmux-server-owner.ts +9 -0
- package/packages/daemon/src/lib/worktree.ts +16 -0
- package/packages/daemon/src/open-browser.ts +59 -18
- package/packages/daemon/src/open-reader.ts +148 -0
- package/packages/daemon/src/plugins/action-catalog.ts +141 -0
- package/packages/daemon/src/plugins/action-cli.ts +248 -0
- package/packages/daemon/src/plugins/api-table.ts +346 -0
- package/packages/daemon/src/plugins/cli.ts +630 -0
- package/packages/daemon/src/plugins/consent.ts +125 -0
- package/packages/daemon/src/plugins/daemon-plugins.ts +172 -0
- package/packages/daemon/src/plugins/discovery.ts +176 -0
- package/packages/daemon/src/plugins/examples/worktree-tasks.ts +120 -0
- package/packages/daemon/src/plugins/exec-sandbox.ts +264 -0
- package/packages/daemon/src/plugins/host/actions-prelude.ts +280 -0
- package/packages/daemon/src/plugins/host/entry.ts +5 -0
- package/packages/daemon/src/plugins/host/host-api-prelude.ts +233 -0
- package/packages/daemon/src/plugins/host/lua-state.ts +557 -0
- package/packages/daemon/src/plugins/host/main.ts +359 -0
- package/packages/daemon/src/plugins/host/sandbox.ts +301 -0
- package/packages/daemon/src/plugins/host-calls.ts +343 -0
- package/packages/daemon/src/plugins/manifest.ts +74 -0
- package/packages/daemon/src/plugins/plugin-exec.ts +326 -0
- package/packages/daemon/src/plugins/plugin-log.ts +63 -0
- package/packages/daemon/src/plugins/plugin-store.ts +80 -0
- package/packages/daemon/src/plugins/protocol.ts +275 -0
- package/packages/daemon/src/plugins/safe-files.ts +105 -0
- package/packages/daemon/src/plugins/supervisor.ts +954 -0
- package/packages/daemon/src/plugins/worktree-calls.ts +183 -0
- package/packages/daemon/src/restore.ts +80 -44
- package/packages/daemon/src/terminal/mirror/events.ts +2 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +6 -1
- package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +15 -2
- package/packages/daemon/src/terminal/protocol/reader-option.ts +77 -0
- package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +10 -1
- package/packages/daemon/src/tmux-upgrade/cli.ts +118 -0
- package/packages/daemon/src/tmux-upgrade/io.ts +596 -0
- package/packages/daemon/src/tmux-upgrade/plan.ts +353 -0
- package/packages/daemon/src/tmux-upgrade/run.ts +325 -0
- package/packages/daemon/src/tui/chrome/snapshot.ts +3 -4
- package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-navigation.ts +110 -10
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +90 -6
- package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-agent-navigation.ts +20 -9
- package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +42 -2
- package/packages/daemon/src/tui/mirror/runtime/application-palette-preview.tsx +7 -1
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-commands.ts +229 -0
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-form.ts +312 -0
- package/packages/daemon/src/tui/mirror/runtime/application-root-v2.tsx +2 -2
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-interaction-controller.ts +149 -128
- package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +44 -2
- package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
- package/packages/tmux-bridge/src/runner.ts +3 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ide.worktree.list|create|open|remove`: git worktrees and their sessions,
|
|
3
|
+
* for plugins granted the `worktree` capability.
|
|
4
|
+
*
|
|
5
|
+
* The daemon runs its own `tmux-ide worktree … --json` (the same code and
|
|
6
|
+
* gates as the CLI), outside the plugin sandbox, with arguments it checked:
|
|
7
|
+
* a branch or ref that git would accept and cannot pass for an option, and
|
|
8
|
+
* a session that exists, by exact name, on the daemon's tmux server. The
|
|
9
|
+
* plugin never gets a program it could aim elsewhere. Sessions it opens are
|
|
10
|
+
* plain: the worktree's own tmux-ide layout (commands from that branch)
|
|
11
|
+
* never runs. `remove` never passes `--force`, so a worktree with changes
|
|
12
|
+
* is refused; plugins mark actions that remove as dangerous so a person
|
|
13
|
+
* confirms them.
|
|
14
|
+
*/
|
|
15
|
+
import { spawn } from "node:child_process";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
|
|
19
|
+
export const WORKTREE_OPERATIONS = ["list", "create", "open", "remove"] as const;
|
|
20
|
+
export type WorktreeOperation = (typeof WORKTREE_OPERATIONS)[number];
|
|
21
|
+
|
|
22
|
+
/** A branch or ref: git-valid, and never something argv parsing reads as an option. */
|
|
23
|
+
const REF = z
|
|
24
|
+
.string()
|
|
25
|
+
.min(1)
|
|
26
|
+
.max(200)
|
|
27
|
+
.regex(/^[A-Za-z0-9_][A-Za-z0-9._/-]*$/u, "must be letters, digits and . _ / - only")
|
|
28
|
+
.refine(
|
|
29
|
+
(ref) =>
|
|
30
|
+
!ref.includes("..") &&
|
|
31
|
+
!ref.includes("//") &&
|
|
32
|
+
!ref.endsWith("/") &&
|
|
33
|
+
!ref.endsWith(".") &&
|
|
34
|
+
!ref.endsWith(".lock") &&
|
|
35
|
+
!ref.split("/").some((part) => part.startsWith(".")),
|
|
36
|
+
"is not a valid git branch name",
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A tmux session name tmux reads literally: letters, digits, space and
|
|
41
|
+
* `_ - @ + ,` only. Nothing tmux takes as a pattern (`* ? [`), a format
|
|
42
|
+
* (`#{`), target syntax (`: . = ~ $ %`) or an option (a leading `-`).
|
|
43
|
+
*/
|
|
44
|
+
const SESSION = z
|
|
45
|
+
.string()
|
|
46
|
+
.min(1)
|
|
47
|
+
.max(200)
|
|
48
|
+
.regex(/^[\p{L}\p{N}_@+][\p{L}\p{N} _@+,-]*$/u, "is not a plain tmux session name");
|
|
49
|
+
|
|
50
|
+
const RequestSchemaZ = z
|
|
51
|
+
.object({
|
|
52
|
+
session: SESSION,
|
|
53
|
+
branch: REF.optional(),
|
|
54
|
+
from: REF.optional(),
|
|
55
|
+
})
|
|
56
|
+
.strict();
|
|
57
|
+
|
|
58
|
+
export interface WorktreeCallDependencies {
|
|
59
|
+
/** Run tmux against the daemon's server and return stdout. */
|
|
60
|
+
readonly tmux: (args: readonly string[]) => Promise<string>;
|
|
61
|
+
/** Whether the calling plugin may still act; checked right before the CLI runs. */
|
|
62
|
+
readonly stillActive?: () => boolean;
|
|
63
|
+
/** Run the tmux-ide CLI with `args` and `env`; resolves with its exit and output. */
|
|
64
|
+
readonly runCli: (
|
|
65
|
+
args: readonly string[],
|
|
66
|
+
env: NodeJS.ProcessEnv,
|
|
67
|
+
) => Promise<{ code: number | null; stdout: string; stderr: string }>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class WorktreeCallError extends Error {}
|
|
71
|
+
|
|
72
|
+
function fail(message: string): never {
|
|
73
|
+
throw new WorktreeCallError(message);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Check a request and run the worktree operation; resolves with tmux-ide's JSON answer. */
|
|
77
|
+
export async function worktreeCall(
|
|
78
|
+
operation: unknown,
|
|
79
|
+
raw: unknown,
|
|
80
|
+
deps: WorktreeCallDependencies,
|
|
81
|
+
): Promise<unknown> {
|
|
82
|
+
if (!WORKTREE_OPERATIONS.includes(operation as WorktreeOperation))
|
|
83
|
+
fail("E_INVALID: ide.worktree supports list, create, open and remove");
|
|
84
|
+
const op = operation as WorktreeOperation;
|
|
85
|
+
const parsed = RequestSchemaZ.safeParse(raw);
|
|
86
|
+
if (!parsed.success) {
|
|
87
|
+
const issue = parsed.error.issues[0];
|
|
88
|
+
fail(
|
|
89
|
+
`E_INVALID: ide.worktree.${op} ${issue?.path.join(".") || "request"} ${issue?.message ?? "is invalid"}`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const { session, branch, from } = parsed.data;
|
|
93
|
+
if (op !== "list" && !branch) fail(`E_INVALID: ide.worktree.${op} needs a branch`);
|
|
94
|
+
if (from && op !== "create") fail(`E_INVALID: only ide.worktree.create takes from`);
|
|
95
|
+
|
|
96
|
+
// The session must exist on the daemon's tmux server, by exact name
|
|
97
|
+
// (`=name`; display-message alone answers even for an unknown session).
|
|
98
|
+
// The CLI then talks to that same server (TMUX names its socket), finds
|
|
99
|
+
// the repository from that session's directory, and fails rather than
|
|
100
|
+
// fall back to its own directory.
|
|
101
|
+
let where: string;
|
|
102
|
+
try {
|
|
103
|
+
await deps.tmux(["has-session", "-t", `=${session}`]);
|
|
104
|
+
// "|" cannot be in a session name (see SESSION), so the first one splits.
|
|
105
|
+
// (Not a tab: tmux prints control characters as "_" under LANG=C.)
|
|
106
|
+
const line = (
|
|
107
|
+
await deps.tmux([
|
|
108
|
+
"display-message",
|
|
109
|
+
"-p",
|
|
110
|
+
"-t",
|
|
111
|
+
`=${session}:`,
|
|
112
|
+
"#{session_name}|#{socket_path},#{pid}",
|
|
113
|
+
])
|
|
114
|
+
).trim();
|
|
115
|
+
const split = line.indexOf("|");
|
|
116
|
+
const name = split < 0 ? line : line.slice(0, split);
|
|
117
|
+
const socket = split < 0 ? "" : line.slice(split + 1);
|
|
118
|
+
if (name !== session || !socket || !/^\/.+,\d+$/u.test(socket)) throw new Error("mismatch");
|
|
119
|
+
where = socket;
|
|
120
|
+
} catch {
|
|
121
|
+
fail(`E_NOT_FOUND: no tmux session '${session}'`);
|
|
122
|
+
}
|
|
123
|
+
if (deps.stillActive && !deps.stillActive())
|
|
124
|
+
fail("E_UNAVAILABLE: the plugin was stopped before this finished");
|
|
125
|
+
|
|
126
|
+
const args = ["worktree", op];
|
|
127
|
+
if (branch) args.push(branch);
|
|
128
|
+
if (from) args.push("--from", from);
|
|
129
|
+
// --plain: a plugin never starts the worktree's own layout, whose commands
|
|
130
|
+
// come from that branch (which `from` may name).
|
|
131
|
+
if (op === "create" || op === "open") args.push("--plain");
|
|
132
|
+
// `=name:`: the exact session (a bare `=name` names no pane for tmux).
|
|
133
|
+
// No git hook runs either: `git worktree add` would run post-checkout, and
|
|
134
|
+
// a relative core.hooksPath takes the hook from the new worktree's branch.
|
|
135
|
+
args.push("--session", `=${session}:`, "--require-session", "--no-git-hooks", "--json");
|
|
136
|
+
const env: NodeJS.ProcessEnv = { ...process.env, TMUX: `${where},0` };
|
|
137
|
+
delete env.TMUX_PANE;
|
|
138
|
+
const result = await deps.runCli(args, env);
|
|
139
|
+
if (result.code !== 0) {
|
|
140
|
+
const message = (result.stderr.trim() || result.stdout.trim()).split("\n").slice(-3).join(" ");
|
|
141
|
+
fail(`E_FAILED: ${message || `tmux-ide worktree ${op} failed`}`.slice(0, 2000));
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(result.stdout) as unknown;
|
|
145
|
+
} catch {
|
|
146
|
+
fail("E_FAILED: tmux-ide worktree sent an unexpected answer");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const OUTPUT_CAP = 1024 * 1024;
|
|
151
|
+
|
|
152
|
+
/** Run `command` (the tmux-ide CLI argv prefix) with `args`, for up to `timeoutMs`. */
|
|
153
|
+
export function cliRunner(
|
|
154
|
+
command: readonly string[],
|
|
155
|
+
timeoutMs = 60_000,
|
|
156
|
+
): WorktreeCallDependencies["runCli"] {
|
|
157
|
+
return (args, env) =>
|
|
158
|
+
new Promise((resolve, reject) => {
|
|
159
|
+
const [file, ...prefix] = command;
|
|
160
|
+
const child = spawn(file!, [...prefix, ...args], {
|
|
161
|
+
cwd: homedir(),
|
|
162
|
+
env,
|
|
163
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
164
|
+
});
|
|
165
|
+
let stdout = "";
|
|
166
|
+
let stderr = "";
|
|
167
|
+
child.stdout.on("data", (chunk: Buffer) => {
|
|
168
|
+
if (stdout.length < OUTPUT_CAP) stdout += chunk.toString("utf8");
|
|
169
|
+
});
|
|
170
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
171
|
+
if (stderr.length < OUTPUT_CAP) stderr += chunk.toString("utf8");
|
|
172
|
+
});
|
|
173
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), timeoutMs);
|
|
174
|
+
child.on("error", (error) => {
|
|
175
|
+
clearTimeout(timer);
|
|
176
|
+
reject(new WorktreeCallError(`E_UNAVAILABLE: could not run tmux-ide: ${error.message}`));
|
|
177
|
+
});
|
|
178
|
+
child.on("close", (code) => {
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
resolve({ code, stdout, stderr });
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
@@ -365,6 +365,23 @@ export interface RestoreOptions {
|
|
|
365
365
|
runCommands?: boolean;
|
|
366
366
|
/** Relaunch resumable agent panes (`--resume-agents`); ORed with config default. */
|
|
367
367
|
resumeAgents?: boolean;
|
|
368
|
+
/** Read this snapshot file instead of `~/.tmux-ide/snapshot.json` (`--from`). */
|
|
369
|
+
from?: string;
|
|
370
|
+
/** Rebuild every session from the snapshot, never from its workspace config. */
|
|
371
|
+
snapshotOnly?: boolean;
|
|
372
|
+
/** OR `resumeAgents` with the config default (default true). The upgrade passes false. */
|
|
373
|
+
resumeFromConfig?: boolean;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** What one restore run did; sessions that threw while rebuilding are in `failed`. */
|
|
377
|
+
export interface RestoreOutcome {
|
|
378
|
+
snapshot: FleetSnapshot;
|
|
379
|
+
plan: RestorePlan;
|
|
380
|
+
restored: string[];
|
|
381
|
+
launched: string[];
|
|
382
|
+
resumed: ResumedSession[];
|
|
383
|
+
failed: string[];
|
|
384
|
+
resumeAgents: boolean;
|
|
368
385
|
}
|
|
369
386
|
|
|
370
387
|
/** One rebuilt session's resumed agent panes (for the report). */
|
|
@@ -380,75 +397,88 @@ interface ResumedSession {
|
|
|
380
397
|
* (flag ORed with the `~/.tmux-ide/config.json` default) revives agent
|
|
381
398
|
* conversations via `claude --resume <id>`.
|
|
382
399
|
*/
|
|
383
|
-
export async function restore({
|
|
400
|
+
export async function restore(options: RestoreOptions = {}): Promise<void> {
|
|
401
|
+
const { json = false, dryRun = false } = options;
|
|
402
|
+
const outcome = await restoreFleet(options);
|
|
403
|
+
reportPlan(outcome.plan, outcome.snapshot, { ...outcome, json, dryRun });
|
|
404
|
+
if (outcome.failed.length) {
|
|
405
|
+
throw new IdeError(`could not rebuild ${outcome.failed.join(", ")}`, {
|
|
406
|
+
code: "RESTORE_FAILED",
|
|
407
|
+
exitCode: 1,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Rebuild the fleet without printing. One session failing to rebuild does not
|
|
414
|
+
* stop the rest; it is listed in `failed`.
|
|
415
|
+
*/
|
|
416
|
+
export async function restoreFleet({
|
|
384
417
|
json = false,
|
|
385
418
|
dryRun = false,
|
|
386
419
|
runCommands = false,
|
|
387
420
|
resumeAgents = false,
|
|
388
|
-
|
|
389
|
-
|
|
421
|
+
from,
|
|
422
|
+
snapshotOnly = false,
|
|
423
|
+
resumeFromConfig = true,
|
|
424
|
+
}: RestoreOptions = {}): Promise<RestoreOutcome> {
|
|
425
|
+
const snapshot = readSnapshot(from);
|
|
390
426
|
if (!snapshot) {
|
|
391
427
|
throw new IdeError(
|
|
392
|
-
|
|
428
|
+
from
|
|
429
|
+
? `no readable snapshot at ${from}`
|
|
430
|
+
: "no snapshot yet — the updater writes one every ~30s while any session is adopted",
|
|
393
431
|
{ code: "NO_SNAPSHOT", exitCode: 1 },
|
|
394
432
|
);
|
|
395
433
|
}
|
|
396
434
|
|
|
397
435
|
// The flag forces resume on; config makes it the default. No negative flag.
|
|
398
|
-
const resume = resumeAgents || readRestorePrefs().resumeAgents;
|
|
399
|
-
const plan = buildRestorePlan(
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
436
|
+
const resume = resumeAgents || (resumeFromConfig && readRestorePrefs().resumeAgents);
|
|
437
|
+
const plan = buildRestorePlan(
|
|
438
|
+
snapshot,
|
|
439
|
+
liveSessions(),
|
|
440
|
+
snapshotOnly ? new Map() : ideBackedProjects(),
|
|
441
|
+
);
|
|
442
|
+
const outcome: RestoreOutcome = {
|
|
443
|
+
snapshot,
|
|
444
|
+
plan,
|
|
445
|
+
restored: [],
|
|
446
|
+
launched: [],
|
|
447
|
+
resumed: [],
|
|
448
|
+
failed: [],
|
|
449
|
+
resumeAgents: resume,
|
|
450
|
+
};
|
|
451
|
+
if (dryRun) return outcome;
|
|
412
452
|
|
|
413
|
-
const restored: string[] = [];
|
|
414
|
-
const launched: string[] = [];
|
|
415
|
-
const resumed: ResumedSession[] = [];
|
|
416
453
|
const recordResumed = (session: string, panes: string[]) => {
|
|
417
|
-
if (panes.length) resumed.push({ session, panes });
|
|
454
|
+
if (panes.length) outcome.resumed.push({ session, panes });
|
|
455
|
+
};
|
|
456
|
+
const rebuild = (session: SessionSnapshot) => {
|
|
457
|
+
try {
|
|
458
|
+
recordResumed(session.name, rebuildSession(session, { runCommands, resumeAgents: resume }));
|
|
459
|
+
} catch {
|
|
460
|
+
outcome.failed.push(session.name);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (session.adopted) safeAdopt(session.name);
|
|
464
|
+
outcome.restored.push(session.name);
|
|
418
465
|
};
|
|
419
466
|
for (const action of plan.actions) {
|
|
420
467
|
if (action.kind === "skip") continue;
|
|
421
468
|
if (action.kind === "launch") {
|
|
422
469
|
const ok = await launchProject(action.dir, json);
|
|
423
|
-
if (ok) launched.push(action.session);
|
|
470
|
+
if (ok) outcome.launched.push(action.session);
|
|
424
471
|
else {
|
|
425
472
|
// Launch failed — fall back to a raw rebuild from the snapshot so the
|
|
426
473
|
// session still comes back.
|
|
427
474
|
const snap = snapshot.sessions.find((s) => s.name === action.session);
|
|
428
|
-
if (snap)
|
|
429
|
-
recordResumed(snap.name, rebuildSession(snap, { runCommands, resumeAgents: resume }));
|
|
430
|
-
if (snap.adopted) safeAdopt(snap.name);
|
|
431
|
-
restored.push(action.session);
|
|
432
|
-
}
|
|
475
|
+
if (snap) rebuild(snap);
|
|
433
476
|
}
|
|
434
477
|
continue;
|
|
435
478
|
}
|
|
436
|
-
|
|
437
|
-
action.session.name,
|
|
438
|
-
rebuildSession(action.session, { runCommands, resumeAgents: resume }),
|
|
439
|
-
);
|
|
440
|
-
if (action.session.adopted) safeAdopt(action.session.name);
|
|
441
|
-
restored.push(action.session.name);
|
|
479
|
+
rebuild(action.session);
|
|
442
480
|
}
|
|
443
|
-
|
|
444
|
-
reportPlan(plan, snapshot, {
|
|
445
|
-
json,
|
|
446
|
-
dryRun: false,
|
|
447
|
-
restored,
|
|
448
|
-
launched,
|
|
449
|
-
resumed,
|
|
450
|
-
resumeAgents: resume,
|
|
451
|
-
});
|
|
481
|
+
return outcome;
|
|
452
482
|
}
|
|
453
483
|
|
|
454
484
|
/** Adopt a restored session into the chrome; never let a chrome failure abort restore. */
|
|
@@ -488,12 +518,14 @@ interface ReportContext {
|
|
|
488
518
|
resumed: ResumedSession[];
|
|
489
519
|
/** Whether resume is on (drives the dry-run `(would resume N agents)` note). */
|
|
490
520
|
resumeAgents: boolean;
|
|
521
|
+
/** Sessions whose rebuild threw. */
|
|
522
|
+
failed: string[];
|
|
491
523
|
}
|
|
492
524
|
|
|
493
525
|
function reportPlan(
|
|
494
526
|
plan: RestorePlan,
|
|
495
527
|
snapshot: FleetSnapshot,
|
|
496
|
-
{ json, dryRun, restored, launched, resumed, resumeAgents }: ReportContext,
|
|
528
|
+
{ json, dryRun, restored, launched, resumed, resumeAgents, failed }: ReportContext,
|
|
497
529
|
): void {
|
|
498
530
|
const skipped = plan.actions.filter((a) => a.kind === "skip").map((a) => a.session);
|
|
499
531
|
const willLaunch = plan.actions.filter((a) => a.kind === "launch").map((a) => a.session);
|
|
@@ -513,6 +545,7 @@ function reportPlan(
|
|
|
513
545
|
resumeAgents,
|
|
514
546
|
resumedPanes,
|
|
515
547
|
resumed,
|
|
548
|
+
failed,
|
|
516
549
|
},
|
|
517
550
|
null,
|
|
518
551
|
2,
|
|
@@ -553,5 +586,8 @@ function reportPlan(
|
|
|
553
586
|
parts.push(`rebuilt ${restored.map((s) => `${s}${resumeSuffix(s)}`).join(", ")}`);
|
|
554
587
|
if (launched.length) parts.push(`launched ${launched.join(", ")}`);
|
|
555
588
|
if (skipped.length) parts.push(`skipped ${skipped.join(", ")} (already running)`);
|
|
589
|
+
if (failed.length) parts.push(`failed to rebuild ${failed.join(", ")}`);
|
|
590
|
+
// Name the snapshot's age: an old snapshot brings back an old fleet (#766).
|
|
591
|
+
console.log(`Snapshot saved ${snapshot.savedAt}.`);
|
|
556
592
|
console.log(parts.length ? `Restored: ${parts.join("; ")}` : "Nothing to restore.");
|
|
557
593
|
}
|
|
@@ -92,6 +92,8 @@ export type MirrorPaneEvent =
|
|
|
92
92
|
/** One visible pane rectangle inside a layout event, in window cells. */
|
|
93
93
|
export interface MirrorLayoutPane {
|
|
94
94
|
browserIcon?: string | null;
|
|
95
|
+
/** A live Markdown reader pane; the path is daemon-only and never framed. */
|
|
96
|
+
reader?: { name: string; path: string } | null;
|
|
95
97
|
/** Null while the pane's semantic identity join is still unverified. */
|
|
96
98
|
semanticPaneId: string | null;
|
|
97
99
|
/** Daemon-authored presentation; clients never infer this from terminal bytes. */
|
|
@@ -753,6 +753,7 @@ export class SessionChannel {
|
|
|
753
753
|
await this.io.start();
|
|
754
754
|
await this.captureAttachedSessionIdentity();
|
|
755
755
|
this.io.send("refresh-client -B 'tmux-ide-browser-icon:%*:#{@tmux_ide_browser_icon}'");
|
|
756
|
+
this.io.send("refresh-client -B 'tmux-ide-reader:%*:#{@tmux_ide_reader}'");
|
|
756
757
|
// Changing border placement can resize PTYs without changing the layout
|
|
757
758
|
// string, so tmux emits no layout-change. Subscribe to this window option.
|
|
758
759
|
this.io.send("refresh-client -B 'tmux-ide-pane-borders:@*:#{pane-border-status}'");
|
|
@@ -2904,7 +2905,10 @@ export class SessionChannel {
|
|
|
2904
2905
|
name === "sessions-changed"
|
|
2905
2906
|
)
|
|
2906
2907
|
this.opts.onWindowTopologyChanged?.();
|
|
2907
|
-
if (
|
|
2908
|
+
if (
|
|
2909
|
+
name === "subscription-changed" &&
|
|
2910
|
+
(rest.startsWith("tmux-ide-browser-icon ") || rest.startsWith("tmux-ide-reader "))
|
|
2911
|
+
) {
|
|
2908
2912
|
this.scheduleSync();
|
|
2909
2913
|
return;
|
|
2910
2914
|
}
|
|
@@ -3325,6 +3329,7 @@ export class SessionChannel {
|
|
|
3325
3329
|
displayName: display?.name ?? null,
|
|
3326
3330
|
displayNameSource: display?.source ?? null,
|
|
3327
3331
|
browserIcon: pane?.descriptor?.browserIcon ?? null,
|
|
3332
|
+
reader: pane?.descriptor?.reader ?? null,
|
|
3328
3333
|
left: leaf.left,
|
|
3329
3334
|
top: leaf.top,
|
|
3330
3335
|
width: leaf.width,
|
|
@@ -22,6 +22,8 @@ import type { LocalImageOwner } from "../graphics/local-image-owner.ts";
|
|
|
22
22
|
import {
|
|
23
23
|
PANE_STREAM_INPUT_LIFETIME_CAPABILITY_VERSION,
|
|
24
24
|
PANE_STREAM_INPUT_BYTES_CAPABILITY_VERSION,
|
|
25
|
+
PANE_STREAM_BROWSER_ICON_CAPABILITY_VERSION,
|
|
26
|
+
PANE_STREAM_READER_CAPABILITY_VERSION,
|
|
25
27
|
type PaneStreamInputLifetime,
|
|
26
28
|
} from "@tmux-ide/contracts";
|
|
27
29
|
import { inflateSync } from "node:zlib";
|
|
@@ -1933,7 +1935,10 @@ export class PaneStreamLiveConnection {
|
|
|
1933
1935
|
// when served.
|
|
1934
1936
|
const zlib =
|
|
1935
1937
|
this.#descriptor.terminalDelivery?.protocolVersions.some(
|
|
1936
|
-
(version) =>
|
|
1938
|
+
(version) =>
|
|
1939
|
+
version === TERMINAL_GRAPHICS_ZLIB_CAPABILITY_VERSION ||
|
|
1940
|
+
version === PANE_STREAM_BROWSER_ICON_CAPABILITY_VERSION ||
|
|
1941
|
+
version === PANE_STREAM_READER_CAPABILITY_VERSION,
|
|
1937
1942
|
) === true;
|
|
1938
1943
|
channel.graphicsZlib = zlib;
|
|
1939
1944
|
channel.graphicsLocal =
|
|
@@ -2414,6 +2419,10 @@ export class PaneStreamLiveConnection {
|
|
|
2414
2419
|
this.#sendFrame(null, frame);
|
|
2415
2420
|
}
|
|
2416
2421
|
|
|
2422
|
+
#offers(version: number): boolean {
|
|
2423
|
+
return this.#descriptor.terminalDelivery?.protocolVersions.includes(version) === true;
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2417
2426
|
#layoutFrame(event: MirrorLayoutEvent): PaneStreamServerFrame | null {
|
|
2418
2427
|
const frame = {
|
|
2419
2428
|
type: "layout",
|
|
@@ -2429,9 +2438,13 @@ export class PaneStreamLiveConnection {
|
|
|
2429
2438
|
pane: pane.semanticPaneId,
|
|
2430
2439
|
displayName: pane.displayName,
|
|
2431
2440
|
displayNameSource: pane.displayNameSource,
|
|
2432
|
-
...(this.#
|
|
2441
|
+
...(this.#offers(PANE_STREAM_BROWSER_ICON_CAPABILITY_VERSION) ||
|
|
2442
|
+
this.#offers(PANE_STREAM_READER_CAPABILITY_VERSION)
|
|
2433
2443
|
? { browserIcon: pane.browserIcon ?? null }
|
|
2434
2444
|
: {}),
|
|
2445
|
+
...(this.#offers(PANE_STREAM_READER_CAPABILITY_VERSION)
|
|
2446
|
+
? { reader: pane.reader ? { name: pane.reader.name } : null }
|
|
2447
|
+
: {}),
|
|
2435
2448
|
left: pane.left,
|
|
2436
2449
|
top: pane.top,
|
|
2437
2450
|
width: pane.width,
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { basename, isAbsolute } from "node:path";
|
|
2
|
+
import { hasControlCharacter } from "@tmux-ide/contracts";
|
|
3
|
+
|
|
4
|
+
export interface ReaderOption {
|
|
5
|
+
/** The file name shown in the pane header. */
|
|
6
|
+
name: string;
|
|
7
|
+
/** Absolute real path; daemon-only, never sent to clients. */
|
|
8
|
+
path: string;
|
|
9
|
+
/** The reader process: the pane's own process. */
|
|
10
|
+
pid: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const MARKDOWN = /\.(?:md|markdown)$/iu;
|
|
14
|
+
|
|
15
|
+
export function isMarkdownPath(path: string): boolean {
|
|
16
|
+
const name = basename(path);
|
|
17
|
+
return MARKDOWN.test(name) && !/^\.(?:md|markdown)$/iu.test(name);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Parse `@tmux_ide_reader`: `{"version":1,"pid":N,"path":"/abs/notes.md"}`.
|
|
22
|
+
* A reader exists only while the process that set the option is alive, and
|
|
23
|
+
* only on the pane whose own process it is: `panePid` is that pane's
|
|
24
|
+
* `#{pane_pid}`. The CLI starts `tmux-ide show` with tmux's direct exec (no
|
|
25
|
+
* shell), so the pane's process is the reader itself and records exactly that
|
|
26
|
+
* pid. Equality rather than "a descendant of" needs no process-tree walk.
|
|
27
|
+
*
|
|
28
|
+
* What the tie rules out: an option left behind by a reader that has exited,
|
|
29
|
+
* and a later process that happens to reuse its pid. What it does not: the
|
|
30
|
+
* option is not proof that `tmux-ide show` set it. In a shell pane the shell
|
|
31
|
+
* is the pane's process, so the shell can set an option naming its own pid,
|
|
32
|
+
* and any process that can talk to the tmux server can read another pane's
|
|
33
|
+
* pid and set that pane's option. Both are the same user, who can already
|
|
34
|
+
* read the file; the reader grants no access they do not have.
|
|
35
|
+
*/
|
|
36
|
+
export function readerOption(
|
|
37
|
+
value: string,
|
|
38
|
+
panePid: string,
|
|
39
|
+
alive = processAlive,
|
|
40
|
+
): ReaderOption | null {
|
|
41
|
+
if (!value || value.length > 8192) return null;
|
|
42
|
+
let parsed: unknown;
|
|
43
|
+
try {
|
|
44
|
+
parsed = JSON.parse(value);
|
|
45
|
+
} catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
49
|
+
const record = parsed as Record<string, unknown>;
|
|
50
|
+
if (Object.keys(record).sort().join(",") !== "path,pid,version") return null;
|
|
51
|
+
const { version, pid, path } = record;
|
|
52
|
+
if (version !== 1 || typeof pid !== "number" || !Number.isSafeInteger(pid) || pid <= 0)
|
|
53
|
+
return null;
|
|
54
|
+
if (!/^[1-9][0-9]*$/u.test(panePid) || String(pid) !== panePid) return null;
|
|
55
|
+
if (
|
|
56
|
+
typeof path !== "string" ||
|
|
57
|
+
path.length > 4096 ||
|
|
58
|
+
!isAbsolute(path) ||
|
|
59
|
+
hasControlCharacter(path) ||
|
|
60
|
+
!isMarkdownPath(path)
|
|
61
|
+
)
|
|
62
|
+
return null;
|
|
63
|
+
const name = basename(path);
|
|
64
|
+
if (name.length > 255) return null;
|
|
65
|
+
if (!alive(pid)) return null;
|
|
66
|
+
return { name, path, pid };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function processAlive(pid: number): boolean {
|
|
70
|
+
try {
|
|
71
|
+
process.kill(pid, 0);
|
|
72
|
+
return true;
|
|
73
|
+
} catch {
|
|
74
|
+
// Including EPERM: a pid now owned by another user is not our reader.
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { browserIcon } from "./browser-icon.ts";
|
|
2
|
+
import { readerOption, type ReaderOption } from "./reader-option.ts";
|
|
2
3
|
|
|
3
4
|
export interface SessionPaneDescriptor {
|
|
4
5
|
browserIcon?: string | null;
|
|
6
|
+
/** A live `tmux-ide open file.md` reader; the path stays daemon-side. */
|
|
7
|
+
reader?: ReaderOption | null;
|
|
5
8
|
nativePaneBirthId?: string | null;
|
|
6
9
|
sessionName: string;
|
|
7
10
|
runtimePaneId: string;
|
|
@@ -56,6 +59,9 @@ export const SESSION_PANE_DESCRIPTOR_FORMAT = [
|
|
|
56
59
|
"#{pane_birth_id}",
|
|
57
60
|
"#{qa:@tmux_ide_name_source}",
|
|
58
61
|
"#{qa:@tmux_ide_browser_icon}",
|
|
62
|
+
"#{qa:@tmux_ide_reader}",
|
|
63
|
+
// The reader option belongs to the pane only when its own process set it.
|
|
64
|
+
"#{pane_pid}",
|
|
59
65
|
].join("\t");
|
|
60
66
|
|
|
61
67
|
/** Decode the escapes emitted by tmux's `qa` format modifier. */
|
|
@@ -135,7 +141,7 @@ export function parseSessionPaneDescriptorReply(
|
|
|
135
141
|
continue;
|
|
136
142
|
}
|
|
137
143
|
const encoded = utf8Line.split("\t");
|
|
138
|
-
if (encoded.length
|
|
144
|
+
if (encoded.length < 21 || encoded.length > 25) continue;
|
|
139
145
|
const [
|
|
140
146
|
runtimePaneId = "",
|
|
141
147
|
semanticPaneId = "",
|
|
@@ -160,6 +166,8 @@ export function parseSessionPaneDescriptorReply(
|
|
|
160
166
|
nativePaneBirthId = "",
|
|
161
167
|
nameSource = "",
|
|
162
168
|
browserIconRaw = "",
|
|
169
|
+
readerRaw = "",
|
|
170
|
+
panePidRaw = "",
|
|
163
171
|
] = encoded.map(decodeTmuxArgument);
|
|
164
172
|
if (!/^%(?:0|[1-9][0-9]*)$/u.test(runtimePaneId) || runtimePaneId.length > 32) continue;
|
|
165
173
|
if (!/^\$(?:0|[1-9][0-9]*)$/u.test(runtimeSessionId) || runtimeSessionId.length > 32) continue;
|
|
@@ -200,6 +208,7 @@ export function parseSessionPaneDescriptorReply(
|
|
|
200
208
|
}
|
|
201
209
|
descriptors.push({
|
|
202
210
|
browserIcon: browserIcon(browserIconRaw),
|
|
211
|
+
reader: readerOption(readerRaw, panePidRaw),
|
|
203
212
|
sessionName,
|
|
204
213
|
runtimePaneId,
|
|
205
214
|
runtimeSessionId,
|