tmux-ide 2.9.18 → 2.9.21
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 +14703 -3882
- 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 +2 -0
- package/packages/contracts/src/plain-text.ts +61 -0
- 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/doctor.js +12 -5
- package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
- package/packages/daemon/dist/lib/daemon-embed.js +24 -1
- package/packages/daemon/dist/lib/headless-daemon.js +1 -0
- package/packages/daemon/dist/lib/native-split-capability.js +13 -15
- 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/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 +132 -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/terminal/mirror/session-channel.js +3 -2
- package/packages/daemon/dist/terminal/mirror/window-split-authority.js +1 -1
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +27 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
- 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/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/doctor.ts +12 -5
- package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
- package/packages/daemon/src/lib/daemon-embed.ts +42 -0
- package/packages/daemon/src/lib/headless-daemon.ts +1 -0
- package/packages/daemon/src/lib/native-split-capability.ts +12 -13
- package/packages/daemon/src/lib/worktree.ts +16 -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 +182 -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/terminal/mirror/session-channel.ts +3 -2
- package/packages/daemon/src/terminal/mirror/window-split-authority.ts +3 -2
- package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +32 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
- 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/workspace/application-command-description.ts +44 -2
- package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the canonical daemon creates its plugin supervisor.
|
|
3
|
+
*/
|
|
4
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { appSettingsPath } from "../lib/app-settings.js";
|
|
7
|
+
import { getCanonicalDaemonInfoPath } from "../lib/canonical-daemon.js";
|
|
8
|
+
import { logger } from "../lib/log.js";
|
|
9
|
+
import { stateHome } from "../lib/state-home.js";
|
|
10
|
+
import { appendPluginLog, pluginLogPath } from "./plugin-log.js";
|
|
11
|
+
import { createHostCallHandler, } from "./host-calls.js";
|
|
12
|
+
import { cliRunner } from "./worktree-calls.js";
|
|
13
|
+
import { defaultControlSocketPath } from "../control/server.js";
|
|
14
|
+
import { DEFAULT_PLUGIN_LIMITS } from "./protocol.js";
|
|
15
|
+
import { minimalHostEnv, PluginSupervisor, secretEnvValues } from "./supervisor.js";
|
|
16
|
+
/**
|
|
17
|
+
* `ide.notify`: a short message in the status line of every attached tmux
|
|
18
|
+
* client (and so in the TUI, which is one), prefixed with the plugin name.
|
|
19
|
+
* `#` is doubled so tmux does not expand formats in plugin text.
|
|
20
|
+
*/
|
|
21
|
+
export async function notifyTmuxClients(tmux, plugin, text, level) {
|
|
22
|
+
const prefix = level === "info" ? "" : `${level}: `;
|
|
23
|
+
const message = `${plugin}: ${prefix}${text}`.replace(/#/gu, "##");
|
|
24
|
+
const clients = (await tmux(["list-clients", "-F", "#{client_name}"]))
|
|
25
|
+
.split("\n")
|
|
26
|
+
.map((line) => line.trim())
|
|
27
|
+
.filter(Boolean);
|
|
28
|
+
await Promise.all(clients.map((client) => tmux(["display-message", "-c", client, "-d", "4000", message]).catch(() => "")));
|
|
29
|
+
}
|
|
30
|
+
/** `TMUX_IDE_PLUGINS=0` turns plugins off for a daemon. */
|
|
31
|
+
export function pluginsEnabled(env = process.env) {
|
|
32
|
+
return env.TMUX_IDE_PLUGINS !== "0";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The argv that starts the plugin host. In the published CLI every module is
|
|
36
|
+
* bundled into `bin/cli.js`, which starts the host with `__plugin-host`.
|
|
37
|
+
* From source, the host entry sits next to this file and inherits the
|
|
38
|
+
* runtime's loader flags (tsx under Node; Bun runs TypeScript directly).
|
|
39
|
+
*/
|
|
40
|
+
export function resolvePluginHostCommand(here = fileURLToPath(import.meta.url), runtime = process) {
|
|
41
|
+
if (basename(here) === "cli.js")
|
|
42
|
+
return [runtime.execPath, here, "__plugin-host"];
|
|
43
|
+
const entry = join(dirname(here), "host", `entry${extname(here)}`);
|
|
44
|
+
return [runtime.execPath, ...runtime.execArgv, entry];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Where tmux keeps sockets: the default directory (under TMUX_TMPDIR or
|
|
48
|
+
* /tmp, per user) and the directory of the daemon's own tmux server.
|
|
49
|
+
*/
|
|
50
|
+
export async function tmuxSocketDirs(tmux, env = process.env, uid = process.getuid?.() ?? 0) {
|
|
51
|
+
const dirs = new Set([join(env.TMUX_TMPDIR || "/tmp", `tmux-${uid}`)]);
|
|
52
|
+
if (env.TMUX_TMPDIR)
|
|
53
|
+
dirs.add(join("/tmp", `tmux-${uid}`));
|
|
54
|
+
try {
|
|
55
|
+
const socket = (await tmux(["list-sessions", "-F", "#{socket_path}"])).split("\n")[0]?.trim();
|
|
56
|
+
// Only a directory of tmux sockets, never a broad one like /tmp itself.
|
|
57
|
+
if (socket?.startsWith("/") && basename(dirname(socket)).startsWith("tmux"))
|
|
58
|
+
dirs.add(dirname(socket));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// No server yet: the default directories still apply.
|
|
62
|
+
}
|
|
63
|
+
return [...dirs];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The argv prefix that runs this tmux-ide's CLI: the bundled `bin/cli.js`,
|
|
67
|
+
* or `bin/cli.ts` under the runtime's loader flags when run from source.
|
|
68
|
+
*/
|
|
69
|
+
export function resolveCliCommand(here = fileURLToPath(import.meta.url), runtime = process) {
|
|
70
|
+
if (basename(here) === "cli.js")
|
|
71
|
+
return [runtime.execPath, here];
|
|
72
|
+
const cli = join(dirname(here), "..", "..", "..", "..", "bin", `cli${extname(here)}`);
|
|
73
|
+
return [runtime.execPath, ...runtime.execArgv, cli];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Files programs from plugins may not read: the daemon record (its port and
|
|
77
|
+
* owner token), the app settings (the remote-access token), the consent
|
|
78
|
+
* records and every plugin's store.
|
|
79
|
+
*/
|
|
80
|
+
export function daemonPrivateFiles(home, daemonInfoPath = getCanonicalDaemonInfoPath()) {
|
|
81
|
+
// Prefixes: daemon.json also covers its .tmp and .retired siblings, and the
|
|
82
|
+
// record follows TMUX_IDE_DAEMON_INFO_DIR / the registry dir, wherever it is.
|
|
83
|
+
const infoDir = dirname(daemonInfoPath);
|
|
84
|
+
return [
|
|
85
|
+
daemonInfoPath,
|
|
86
|
+
join(infoDir, "daemon.claim"),
|
|
87
|
+
join(home, "consent"),
|
|
88
|
+
join(home, "plugin-store"),
|
|
89
|
+
appSettingsPath(),
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* What the daemon's host functions run on: its state home, its own HTTP
|
|
94
|
+
* endpoint and tmux server, and what programs are kept from (the private
|
|
95
|
+
* files, the tmux socket directories, the control socket and, through the
|
|
96
|
+
* endpoint, the daemon's port).
|
|
97
|
+
*/
|
|
98
|
+
export function daemonHostCallDependencies(options) {
|
|
99
|
+
const tmux = options.tmux ?? (async () => Promise.reject(new Error("tmux is unavailable")));
|
|
100
|
+
return {
|
|
101
|
+
stateHome: options.home,
|
|
102
|
+
endpoint: options.endpoint ?? (() => null),
|
|
103
|
+
tmux,
|
|
104
|
+
baseEnv: () => minimalHostEnv(),
|
|
105
|
+
secrets: options.secretSet,
|
|
106
|
+
notify: (plugin, text, level) => notifyTmuxClients(tmux, plugin, text, level),
|
|
107
|
+
unreadable: () => daemonPrivateFiles(options.home),
|
|
108
|
+
execMaxTimeoutMs: DEFAULT_PLUGIN_LIMITS.wallClockMs,
|
|
109
|
+
runCli: cliRunner(resolveCliCommand()),
|
|
110
|
+
tmuxSocketDirs: () => tmuxSocketDirs(tmux),
|
|
111
|
+
privateSockets: () => [defaultControlSocketPath()],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function createDaemonPluginSupervisor(options) {
|
|
115
|
+
const secrets = options.secrets.filter((secret) => typeof secret === "string");
|
|
116
|
+
const home = options.home ?? stateHome();
|
|
117
|
+
const logPath = pluginLogPath(home);
|
|
118
|
+
const secretSet = () => new Set([...secrets, ...secretEnvValues()].filter((secret) => secret.length >= 8));
|
|
119
|
+
const calls = createHostCallHandler(daemonHostCallDependencies({ home, endpoint: options.endpoint, tmux: options.tmux, secretSet }));
|
|
120
|
+
return new PluginSupervisor({
|
|
121
|
+
calls,
|
|
122
|
+
stateHome: home,
|
|
123
|
+
productVersion: options.productVersion,
|
|
124
|
+
hostCommand: resolvePluginHostCommand(),
|
|
125
|
+
secrets: () => [...secrets, ...secretEnvValues()],
|
|
126
|
+
...(options.onCatalogChange ? { onCatalogChange: options.onCatalogChange } : {}),
|
|
127
|
+
onLog: (entry) => {
|
|
128
|
+
appendPluginLog(logPath, entry);
|
|
129
|
+
logger[entry.level](`plugin.${entry.plugin}`, entry.message, entry.fields);
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find what to load: plugin directories under `<state home>/plugins/` in name
|
|
3
|
+
* order, then the user's `<state home>/init.lua` last, so the user's file can
|
|
4
|
+
* build on and override what plugins set up.
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
7
|
+
import { isAbsolute, join, relative } from "node:path";
|
|
8
|
+
import { consentState, hashPluginDirectory, readConsent } from "./consent.js";
|
|
9
|
+
import { apiSatisfies, manifestDefaults, PluginManifestSchemaZ, } from "./manifest.js";
|
|
10
|
+
import { INIT_PLUGIN_NAME, PLUGIN_NAME_PATTERN, RESERVED_PLUGIN_NAMES, } from "./protocol.js";
|
|
11
|
+
export function pluginPaths(stateHome) {
|
|
12
|
+
return {
|
|
13
|
+
init: join(stateHome, "init.lua"),
|
|
14
|
+
plugins: join(stateHome, "plugins"),
|
|
15
|
+
lua: join(stateHome, "lua"),
|
|
16
|
+
logs: join(stateHome, "logs"),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function inside(child, parent) {
|
|
20
|
+
const rel = relative(parent, child);
|
|
21
|
+
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
|
|
22
|
+
}
|
|
23
|
+
const MAX_MANIFEST_BYTES = 64 * 1024;
|
|
24
|
+
export function discoverPlugins(stateHome) {
|
|
25
|
+
const paths = pluginPaths(stateHome);
|
|
26
|
+
const specs = [];
|
|
27
|
+
const problems = [];
|
|
28
|
+
const awaitingConsent = [];
|
|
29
|
+
const consent = readConsent(stateHome);
|
|
30
|
+
let entries;
|
|
31
|
+
try {
|
|
32
|
+
entries = readdirSync(paths.plugins).sort();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
entries = [];
|
|
36
|
+
}
|
|
37
|
+
for (const dirName of entries) {
|
|
38
|
+
if (dirName.startsWith("."))
|
|
39
|
+
continue;
|
|
40
|
+
const dir = join(paths.plugins, dirName);
|
|
41
|
+
const problem = (error) => problems.push({ name: dirName, path: dir, error });
|
|
42
|
+
try {
|
|
43
|
+
if (!statSync(dir).isDirectory())
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (!PLUGIN_NAME_PATTERN.test(dirName)) {
|
|
50
|
+
problem("directory name must be lowercase letters, digits and dashes");
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (RESERVED_PLUGIN_NAMES.has(dirName)) {
|
|
54
|
+
problem(`'${dirName}' is reserved for tmux-ide's own use; rename the directory`);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const manifestPath = join(dir, "manifest.json");
|
|
58
|
+
let raw;
|
|
59
|
+
try {
|
|
60
|
+
if (statSync(manifestPath).size > MAX_MANIFEST_BYTES) {
|
|
61
|
+
problem("manifest.json is larger than 64 KiB");
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
raw = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
problem(existsSync(manifestPath)
|
|
68
|
+
? `manifest.json is not valid JSON: ${error.message}`
|
|
69
|
+
: "manifest.json is missing");
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const parsed = PluginManifestSchemaZ.safeParse(raw);
|
|
73
|
+
if (!parsed.success) {
|
|
74
|
+
const issue = parsed.error.issues[0];
|
|
75
|
+
problem(`manifest.json: ${issue?.path.join(".") || "(root)"}: ${issue?.message ?? "invalid"}`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const manifest = parsed.data;
|
|
79
|
+
if (manifest.name !== dirName) {
|
|
80
|
+
problem(`manifest name '${manifest.name}' does not match its directory '${dirName}'`);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (!apiSatisfies(manifest.api)) {
|
|
84
|
+
problem(`needs Lua API ${manifest.api}, this tmux-ide provides a different version`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const entry = join(dir, manifest.entry);
|
|
88
|
+
try {
|
|
89
|
+
const realDir = realpathSync(dir);
|
|
90
|
+
const realEntry = realpathSync(entry);
|
|
91
|
+
if (!inside(realEntry, realDir)) {
|
|
92
|
+
problem(`entry '${manifest.entry}' resolves outside the plugin directory`);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
problem(`entry '${manifest.entry}' is missing`);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
let hash;
|
|
101
|
+
try {
|
|
102
|
+
hash = hashPluginDirectory(dir);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
problem(`could not read the plugin: ${error.message}`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const consented = consentState(consent, manifest.name, hash);
|
|
109
|
+
if (consented !== "consented") {
|
|
110
|
+
awaitingConsent.push({ name: manifest.name, path: dir, reason: consented, hash, manifest });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
specs.push({
|
|
114
|
+
name: manifest.name,
|
|
115
|
+
kind: "plugin",
|
|
116
|
+
contentHash: hash,
|
|
117
|
+
entry,
|
|
118
|
+
// Only the plugin's own directory: consent hashes exactly what it can load.
|
|
119
|
+
includeRoots: [dir],
|
|
120
|
+
moduleRoots: [dir],
|
|
121
|
+
settings: manifestDefaults(manifest),
|
|
122
|
+
capabilities: {
|
|
123
|
+
exec: [...(manifest.capabilities.exec ?? [])],
|
|
124
|
+
shell: manifest.capabilities.shell === true,
|
|
125
|
+
panes: manifest.capabilities["panes.send"] === true,
|
|
126
|
+
worktree: manifest.capabilities.worktree === true,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (existsSync(paths.init)) {
|
|
131
|
+
specs.push({
|
|
132
|
+
name: INIT_PLUGIN_NAME,
|
|
133
|
+
kind: "init",
|
|
134
|
+
entry: paths.init,
|
|
135
|
+
includeRoots: [stateHome, paths.lua],
|
|
136
|
+
moduleRoots: [paths.lua],
|
|
137
|
+
settings: {},
|
|
138
|
+
capabilities: { exec: "any", shell: true, panes: true, worktree: true },
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return { specs, problems, awaitingConsent };
|
|
142
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The "worktree tasks" example plugin: tasks as git worktrees, each with its
|
|
3
|
+
* own tmux session, through tmux-ide's own worktree code (`ide.worktree`).
|
|
4
|
+
*
|
|
5
|
+
* `tmux-ide plugin example worktree-tasks` copies these files into
|
|
6
|
+
* ~/.tmux-ide/plugins/worktree-tasks; it then runs only after
|
|
7
|
+
* `tmux-ide plugin consent worktree-tasks`.
|
|
8
|
+
*
|
|
9
|
+
* It asks only for the `worktree` capability and runs no programs: the
|
|
10
|
+
* daemon does the worktree work, for the session the command was run from
|
|
11
|
+
* (`ctx.session`). It never types into a pane, and cannot, so it works the
|
|
12
|
+
* same from the TUI, the CLI and the Mac app.
|
|
13
|
+
*/
|
|
14
|
+
export const WORKTREE_TASKS_MANIFEST = {
|
|
15
|
+
name: "worktree-tasks",
|
|
16
|
+
version: "1.0.0",
|
|
17
|
+
api: "^1.0",
|
|
18
|
+
description: "Tasks as git worktrees: start, open and remove them from the palette, with tmux-ide worktree.",
|
|
19
|
+
capabilities: { worktree: true },
|
|
20
|
+
};
|
|
21
|
+
export const WORKTREE_TASKS_INIT_LUA = String.raw `-- worktree-tasks: tasks as git worktrees, each with its own tmux session.
|
|
22
|
+
-- Every action asks the daemon (ide.worktree) to work on the repository of
|
|
23
|
+
-- the session the command was run from.
|
|
24
|
+
|
|
25
|
+
local function session_of(ctx)
|
|
26
|
+
if not ctx.session then
|
|
27
|
+
error("Run this from a tmux-ide session: it works on that session's repository " ..
|
|
28
|
+
"(from the CLI, run it inside tmux or pass --session).", 0)
|
|
29
|
+
end
|
|
30
|
+
return ctx.session
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
-- One ide.worktree operation for ctx's session. Errors from the daemon
|
|
34
|
+
-- (a refused branch name, a worktree with changes) reach the palette as is.
|
|
35
|
+
local function worktree(ctx, op, spec)
|
|
36
|
+
spec = spec or {}
|
|
37
|
+
spec.session = session_of(ctx)
|
|
38
|
+
return ide.worktree[op](spec)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
-- The task worktrees (not the main checkout), as palette choices.
|
|
42
|
+
local function task_choices(ctx)
|
|
43
|
+
local listed = worktree(ctx, "list")
|
|
44
|
+
local choices = {}
|
|
45
|
+
for _, entry in ipairs(listed.worktrees or {}) do
|
|
46
|
+
if not entry.primary and entry.branch then
|
|
47
|
+
local state = entry.running and ((entry.status or "running") .. " · " .. entry.session) or "no session"
|
|
48
|
+
choices[#choices + 1] = { value = entry.branch, label = entry.branch .. " · " .. state }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
return choices
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
local task_param = { name = "task", label = "Task", type = "choice", choices = task_choices }
|
|
55
|
+
|
|
56
|
+
ide.action.register{
|
|
57
|
+
id = "new",
|
|
58
|
+
title = "New task",
|
|
59
|
+
category = "Tasks",
|
|
60
|
+
description = "Create a worktree on a new branch and open a session in it",
|
|
61
|
+
params = {
|
|
62
|
+
{ name = "branch", label = "Branch name", pattern = "^[%w][%w._/-]*$" },
|
|
63
|
+
{ name = "from", label = "Start from (branch or commit)", optional = true },
|
|
64
|
+
},
|
|
65
|
+
run = function(ctx, args)
|
|
66
|
+
local made = worktree(ctx, "create", { branch = args.branch, from = args.from })
|
|
67
|
+
return "Started " .. made.branch .. " in session " .. tostring(made.session)
|
|
68
|
+
end,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ide.action.register{
|
|
72
|
+
id = "open",
|
|
73
|
+
title = "Open task",
|
|
74
|
+
category = "Tasks",
|
|
75
|
+
description = "Open (or start) the session of a task's worktree",
|
|
76
|
+
params = { task_param },
|
|
77
|
+
run = function(ctx, args)
|
|
78
|
+
local opened = worktree(ctx, "open", { branch = args.task })
|
|
79
|
+
return (opened.created and "Opened " or "Already open: ") .. opened.session
|
|
80
|
+
end,
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
ide.action.register{
|
|
84
|
+
id = "remove",
|
|
85
|
+
title = "Remove task",
|
|
86
|
+
category = "Tasks",
|
|
87
|
+
description = "Close a task's session and remove its worktree (refused if it has changes)",
|
|
88
|
+
dangerous = true,
|
|
89
|
+
params = { task_param },
|
|
90
|
+
run = function(ctx, args)
|
|
91
|
+
local removed = worktree(ctx, "remove", { branch = args.task })
|
|
92
|
+
return "Removed " .. removed.branch .. (removed.sessionKilled and " and closed its session" or "")
|
|
93
|
+
end,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
ide.action.register{
|
|
97
|
+
id = "list",
|
|
98
|
+
title = "List tasks",
|
|
99
|
+
category = "Tasks",
|
|
100
|
+
run = function(ctx)
|
|
101
|
+
local choices = task_choices(ctx)
|
|
102
|
+
if #choices == 0 then return "No tasks yet. Start one with New task." end
|
|
103
|
+
local lines = {}
|
|
104
|
+
for _, choice in ipairs(choices) do lines[#lines + 1] = choice.label end
|
|
105
|
+
return table.concat(lines, "\n")
|
|
106
|
+
end,
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
/** The examples `tmux-ide plugin example` can install, by name. */
|
|
110
|
+
export const PLUGIN_EXAMPLES = {
|
|
111
|
+
"worktree-tasks": {
|
|
112
|
+
manifest: WORKTREE_TASKS_MANIFEST,
|
|
113
|
+
files: { "init.lua": WORKTREE_TASKS_INIT_LUA },
|
|
114
|
+
},
|
|
115
|
+
};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OS sandbox every program a plugin runs goes through.
|
|
3
|
+
*
|
|
4
|
+
* It is a seat belt, not the security boundary: consent is. A program a
|
|
5
|
+
* plugin may run can usually do anything your account can (edit your shell
|
|
6
|
+
* profile, start other programs, talk to the network). What the sandbox
|
|
7
|
+
* adds:
|
|
8
|
+
*
|
|
9
|
+
* - no writes under the tmux-ide state home except the plugin's own data
|
|
10
|
+
* directory, so a program cannot simply edit consent records, init.lua or
|
|
11
|
+
* installed plugins in place;
|
|
12
|
+
* - no reads of the daemon record and credential files the daemon names;
|
|
13
|
+
* - on Linux, its own PID namespace and /proc, so it cannot reach the
|
|
14
|
+
* daemon through /proc/<pid>.
|
|
15
|
+
*
|
|
16
|
+
* macOS uses sandbox-exec. Linux uses bubblewrap, but running programs on
|
|
17
|
+
* Linux is off unless TMUX_IDE_PLUGIN_EXEC_LINUX=1: it has not been tested
|
|
18
|
+
* on a real Linux machine yet. Elsewhere, plugins cannot run programs.
|
|
19
|
+
*/
|
|
20
|
+
import { accessSync, constants, existsSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
21
|
+
import { basename, delimiter, dirname, isAbsolute, join } from "node:path";
|
|
22
|
+
function isDirectory(path) {
|
|
23
|
+
try {
|
|
24
|
+
return statSync(path).isDirectory();
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The environment variable that turns plugin programs on for Linux. */
|
|
31
|
+
export const LINUX_EXEC_OPT_IN = "TMUX_IDE_PLUGIN_EXEC_LINUX";
|
|
32
|
+
/** Resolve symlinks (macOS /tmp is /private/tmp), including for paths that do not exist yet. */
|
|
33
|
+
export function realPath(path) {
|
|
34
|
+
if (existsSync(path))
|
|
35
|
+
return realpathSync(path);
|
|
36
|
+
const parent = dirname(path);
|
|
37
|
+
return parent === path ? path : join(realPath(parent), basename(path));
|
|
38
|
+
}
|
|
39
|
+
/** An SBPL regex literal matching every path that starts with `prefix`. */
|
|
40
|
+
export function sbplPrefixRegex(prefix) {
|
|
41
|
+
const escaped = prefix.replace(/[\\^$.*+?()[\]{}|"]/gu, (char) => `\\${char}`);
|
|
42
|
+
return `#"^${escaped}"`;
|
|
43
|
+
}
|
|
44
|
+
/** `paths` and every directory above them, short of the root, each once. */
|
|
45
|
+
export function immovable(paths) {
|
|
46
|
+
const out = new Set();
|
|
47
|
+
for (const path of paths) {
|
|
48
|
+
for (let current = path; current !== dirname(current); current = dirname(current))
|
|
49
|
+
out.add(current);
|
|
50
|
+
}
|
|
51
|
+
return [...out];
|
|
52
|
+
}
|
|
53
|
+
/** Existing paths that start with `prefix` (its directory's matching entries). */
|
|
54
|
+
function existingWithPrefix(prefix) {
|
|
55
|
+
const dir = dirname(prefix);
|
|
56
|
+
const name = basename(prefix);
|
|
57
|
+
try {
|
|
58
|
+
return readdirSync(dir)
|
|
59
|
+
.filter((entry) => entry.startsWith(name))
|
|
60
|
+
.map((entry) => join(dir, entry));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function sbplString(value) {
|
|
67
|
+
return `"${value.replace(/\\/gu, "\\\\").replace(/"/gu, '\\"')}"`;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The data directory as the sandbox should name it: its parent resolved,
|
|
71
|
+
* its own name kept. If something replaced the directory with a symlink,
|
|
72
|
+
* this still names the link, so the link's target never becomes writable.
|
|
73
|
+
*/
|
|
74
|
+
export function writablePath(paths, resolve = realPath) {
|
|
75
|
+
return join(resolve(dirname(paths.writable)), basename(paths.writable));
|
|
76
|
+
}
|
|
77
|
+
/** The sandbox-exec profile: allow everything, deny writes under the state home but the data dir. */
|
|
78
|
+
export function darwinProfile(paths, resolve = realPath) {
|
|
79
|
+
// Later rules win in SBPL, so the narrower allow comes after the deny,
|
|
80
|
+
// and the read denials come last.
|
|
81
|
+
return [
|
|
82
|
+
"(version 1)",
|
|
83
|
+
"(allow default)",
|
|
84
|
+
`(deny file-write* (subpath ${sbplString(resolve(paths.stateHome))}))`,
|
|
85
|
+
`(allow file-write* (subpath ${sbplString(writablePath(paths, resolve))}))`,
|
|
86
|
+
...(paths.unreadable ?? []).map((path) => `(deny file-read* file-write* (regex ${sbplPrefixRegex(resolve(path))}))`),
|
|
87
|
+
...(paths.tmuxSockets ?? []).flatMap((dir) => {
|
|
88
|
+
const inside = sbplPrefixRegex(`${resolve(dir)}/`);
|
|
89
|
+
return [
|
|
90
|
+
`(deny network-outbound (remote unix-socket (path-regex ${inside})))`,
|
|
91
|
+
`(deny file-read* file-write* (regex ${inside}))`,
|
|
92
|
+
];
|
|
93
|
+
}),
|
|
94
|
+
...(paths.privateSockets ?? []).map((path) => `(deny network-outbound (remote unix-socket (path-regex ${sbplPrefixRegex(resolve(path))})))`),
|
|
95
|
+
// A path rule is only as good as the path: the state home, the private
|
|
96
|
+
// files, the socket directories, the control socket and every directory
|
|
97
|
+
// above them cannot be renamed, removed or relinked, so none of them can
|
|
98
|
+
// be moved out from under the rules above. (Only the paths themselves:
|
|
99
|
+
// files inside those directories are governed by the rules above.)
|
|
100
|
+
...immovable([
|
|
101
|
+
paths.stateHome,
|
|
102
|
+
...(paths.unreadable ?? []),
|
|
103
|
+
...(paths.tmuxSockets ?? []),
|
|
104
|
+
...(paths.privateSockets ?? []),
|
|
105
|
+
].map(resolve)).map((path) => `(deny file-write* (literal ${sbplString(path)}))`),
|
|
106
|
+
...(paths.daemonPorts ?? [])
|
|
107
|
+
.filter((port) => Number.isInteger(port) && port > 0 && port < 65536)
|
|
108
|
+
.map((port) => `(deny network-outbound (remote ip "*:${port}"))`),
|
|
109
|
+
].join("");
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The first executable regular file called `name` in `path`'s directories.
|
|
113
|
+
* Relative entries (".", "bin", empty) are skipped: they would resolve
|
|
114
|
+
* against whatever directory the program starts in.
|
|
115
|
+
*/
|
|
116
|
+
export function findOnPath(name, path = process.env.PATH ?? "") {
|
|
117
|
+
for (const dir of path.split(delimiter)) {
|
|
118
|
+
if (!dir || !isAbsolute(dir))
|
|
119
|
+
continue;
|
|
120
|
+
const candidate = join(dir, name);
|
|
121
|
+
try {
|
|
122
|
+
if (statSync(candidate).isFile()) {
|
|
123
|
+
accessSync(candidate, constants.X_OK);
|
|
124
|
+
return candidate;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
// Not there, or not executable: keep looking.
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
/** Wrap `command` so it runs inside the sandbox, or say why it cannot. */
|
|
134
|
+
export function sandboxedArgv(command, paths, options = {}) {
|
|
135
|
+
const platform = options.platform ?? process.platform;
|
|
136
|
+
const find = options.find ?? ((name) => findOnPath(name));
|
|
137
|
+
const exists = options.exists ?? existsSync;
|
|
138
|
+
const resolve = options.resolve ?? realPath;
|
|
139
|
+
if (platform === "darwin") {
|
|
140
|
+
const tool = "/usr/bin/sandbox-exec";
|
|
141
|
+
if (!exists(tool))
|
|
142
|
+
return { ok: false, reason: "sandbox-exec is missing" };
|
|
143
|
+
return { ok: true, argv: [tool, "-p", darwinProfile(paths, resolve), ...command] };
|
|
144
|
+
}
|
|
145
|
+
if (platform === "linux") {
|
|
146
|
+
const optIn = options.linuxOptIn ?? process.env[LINUX_EXEC_OPT_IN] === "1";
|
|
147
|
+
if (!optIn)
|
|
148
|
+
return {
|
|
149
|
+
ok: false,
|
|
150
|
+
reason: `running programs from plugins on Linux is untested and off; set ${LINUX_EXEC_OPT_IN}=1 for the daemon to turn it on`,
|
|
151
|
+
};
|
|
152
|
+
const tool = find("bwrap");
|
|
153
|
+
if (!tool)
|
|
154
|
+
return {
|
|
155
|
+
ok: false,
|
|
156
|
+
reason: "running programs from plugins needs bubblewrap (bwrap) on Linux",
|
|
157
|
+
};
|
|
158
|
+
const home = resolve(paths.stateHome);
|
|
159
|
+
const writable = writablePath(paths, resolve);
|
|
160
|
+
// bubblewrap binds paths, not patterns: what matches a prefix now is
|
|
161
|
+
// hidden; a file created under that prefix later is not (Linux is off
|
|
162
|
+
// by default and untested).
|
|
163
|
+
const hidden = (paths.unreadable ?? [])
|
|
164
|
+
.map(resolve)
|
|
165
|
+
.flatMap((prefix) => (exists(prefix) ? [prefix] : []).concat(existingWithPrefix(prefix).filter((path) => path !== prefix)));
|
|
166
|
+
return {
|
|
167
|
+
ok: true,
|
|
168
|
+
argv: [
|
|
169
|
+
tool,
|
|
170
|
+
"--dev-bind",
|
|
171
|
+
"/",
|
|
172
|
+
"/",
|
|
173
|
+
"--ro-bind",
|
|
174
|
+
home,
|
|
175
|
+
home,
|
|
176
|
+
"--bind",
|
|
177
|
+
writable,
|
|
178
|
+
writable,
|
|
179
|
+
// Hide each unreadable file behind an empty one, each directory
|
|
180
|
+
// behind an empty tmpfs.
|
|
181
|
+
...hidden.flatMap((path) => isDirectory(path) ? ["--tmpfs", path] : ["--ro-bind", "/dev/null", path]),
|
|
182
|
+
// tmux socket directories become empty: no tmux server to reach.
|
|
183
|
+
...(paths.tmuxSockets ?? [])
|
|
184
|
+
.map(resolve)
|
|
185
|
+
.filter((dir) => exists(dir) && isDirectory(dir))
|
|
186
|
+
.flatMap((dir) => ["--tmpfs", dir]),
|
|
187
|
+
// Its own PIDs and /proc: the daemon's /proc/<pid> (environ, root,
|
|
188
|
+
// fds) is out of sight. Its own IPC and session (no TIOCSTI into
|
|
189
|
+
// the daemon's terminal), and it dies with the daemon.
|
|
190
|
+
"--unshare-pid",
|
|
191
|
+
"--proc",
|
|
192
|
+
"/proc",
|
|
193
|
+
"--unshare-ipc",
|
|
194
|
+
"--new-session",
|
|
195
|
+
"--die-with-parent",
|
|
196
|
+
"--",
|
|
197
|
+
...command,
|
|
198
|
+
],
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
return { ok: false, reason: `plugins cannot run programs on ${platform}` };
|
|
202
|
+
}
|