@termwire/tmux 0.3.0 → 0.4.1

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 CHANGED
@@ -1,5 +1,35 @@
1
1
  # @termwire/tmux
2
2
 
3
+ ## 0.4.1
4
+
5
+ ## 0.4.0
6
+
7
+ ### Minor Changes
8
+
9
+ - 7e73a29: Closing a workspace session now kills the processes started in it.
10
+
11
+ `tmux kill-session` only sends `SIGHUP` to the foreground process group of each
12
+ pane, so background agent jobs detached from the terminal survived it and were
13
+ adopted by `init`. They accumulated for weeks, in one case 158 processes across
14
+ nine abandoned sessions, five of whose worktrees no longer existed on disk.
15
+
16
+ `up` now installs one global tmux `session-closed` hook that runs a hidden
17
+ `termwire _reap <session>` subcommand. The reap collects the processes of the
18
+ current user whose environment still carries `TERMWIRE_SESSION=<session>`, sends
19
+ `SIGTERM`, then confirms the survivors still carry the label before escalating
20
+ them to `SIGKILL`. It signals exactly that list of pids and never a pattern
21
+ match, and never the tmux server or anything it descends from. Each run appends
22
+ one line to `$XDG_STATE_HOME/termwire/reap.log`, because a tmux hook's output is
23
+ shown nowhere.
24
+
25
+ Installing the hook is idempotent. A repeated `up` updates our entry when the
26
+ interpreter or script path changed, drops duplicates of ours, and leaves a
27
+ `session-closed` hook of your own untouched. A tmux server that refuses the hook
28
+ only produces a warning; the workspace still comes up.
29
+
30
+ `@termwire/tmux` gains `showHooks`, `setHook`, and `unsetHook`. It keeps no
31
+ knowledge of what the hooks are for.
32
+
3
33
  ## 0.3.0
4
34
 
5
35
  ### Minor Changes
package/README.md CHANGED
@@ -26,6 +26,7 @@ tmux binary.
26
26
  `setEnvironment`, `attach`.
27
27
  - Windows: `newWindow`, `selectWindow`, `selectLayout`.
28
28
  - Panes: `splitPane`, `respawnPane`, `selectPane`, `sendKeys`.
29
+ - Hooks: `showHooks`, `setHook`, `unsetHook`.
29
30
 
30
31
  `attach` uses `attach-session` outside tmux and `switch-client` when `TMUX` is
31
32
  set, so attaching works the same from a plain terminal and from inside another
@@ -35,6 +36,10 @@ session.
35
36
  uses those ids to place panes and to record the editor pane in
36
37
  `TERMWIRE_EDITOR_PANE`.
37
38
 
39
+ `showHooks` returns the parsed entries, including the array index tmux stores
40
+ each hook under, which is what a caller needs to replace or remove one in place.
41
+ Deciding which hook belongs to whom is not this package's business.
42
+
38
43
  ## Boundary
39
44
 
40
45
  This package owns tmux commands only. Layout interpretation, editor roles,
@@ -1,3 +1,4 @@
1
+ import { setHook, showHooks, unsetHook } from "./hook.js";
1
2
  import { respawnPane, sendKeys, splitPane } from "./pane.js";
2
3
  import { type Exec } from "./process.js";
3
4
  import { newSession } from "./session.js";
@@ -20,4 +21,7 @@ export declare function createTmux({ exec, env }?: CreateTmuxOptions): {
20
21
  selectLayout: (target: string, layout: Parameters<typeof selectLayout>[2]) => Promise<void>;
21
22
  selectPane: (target: string) => Promise<void>;
22
23
  attach: (session: string) => Promise<void>;
24
+ showHooks: (options?: Parameters<typeof showHooks>[1]) => Promise<import("./hook.js").HookEntry[]>;
25
+ setHook: (options: Parameters<typeof setHook>[1]) => Promise<void>;
26
+ unsetHook: (options: Parameters<typeof unsetHook>[1]) => Promise<void>;
23
27
  };
@@ -1,4 +1,5 @@
1
1
  import { attach } from "./client.js";
2
+ import { setHook, showHooks, unsetHook } from "./hook.js";
2
3
  import { respawnPane, selectPane, sendKeys, splitPane } from "./pane.js";
3
4
  import { spawnExec } from "./process.js";
4
5
  import { hasSession, killSession, newSession, setEnvironment, setSessionTitle } from "./session.js";
@@ -18,5 +19,8 @@ export function createTmux({ exec = spawnExec, env = process.env } = {}) {
18
19
  selectLayout: (target, layout) => selectLayout(exec, target, layout),
19
20
  selectPane: (target) => selectPane(exec, target),
20
21
  attach: (session) => attach(exec, env, session),
22
+ showHooks: (options) => showHooks(exec, options),
23
+ setHook: (options) => setHook(exec, options),
24
+ unsetHook: (options) => unsetHook(exec, options),
21
25
  };
22
26
  }
package/dist/hook.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { type Exec } from "./process.js";
2
+ export interface HookEntry {
3
+ name: string;
4
+ index?: number;
5
+ command: string;
6
+ }
7
+ export interface ShowHooksOptions {
8
+ global?: boolean;
9
+ }
10
+ export interface SetHookOptions {
11
+ name: string;
12
+ command: string;
13
+ global?: boolean;
14
+ append?: boolean;
15
+ }
16
+ export interface UnsetHookOptions {
17
+ name: string;
18
+ global?: boolean;
19
+ }
20
+ export declare function parseHooks(stdout: string): HookEntry[];
21
+ export declare function showHooks(exec: Exec, options?: ShowHooksOptions): Promise<HookEntry[]>;
22
+ export declare function setHook(exec: Exec, options: SetHookOptions): Promise<void>;
23
+ export declare function unsetHook(exec: Exec, options: UnsetHookOptions): Promise<void>;
package/dist/hook.js ADDED
@@ -0,0 +1,58 @@
1
+ import { CommandError, execute } from "./process.js";
2
+ import { assertNotEmpty } from "./validation.js";
3
+ export function parseHooks(stdout) {
4
+ const entries = [];
5
+ for (const line of stdout.split("\n")) {
6
+ const trimmed = line.trim();
7
+ if (trimmed.length === 0)
8
+ continue;
9
+ const separator = trimmed.indexOf(" ");
10
+ const key = separator === -1 ? trimmed : trimmed.slice(0, separator);
11
+ const command = separator === -1 ? "" : trimmed.slice(separator + 1);
12
+ const parsed = /^([A-Za-z0-9_-]+)(?:\[(\d+)\])?$/.exec(key);
13
+ if (!parsed)
14
+ continue;
15
+ const index = parsed[2];
16
+ entries.push({
17
+ name: parsed[1],
18
+ ...(index === undefined ? {} : { index: Number(index) }),
19
+ command,
20
+ });
21
+ }
22
+ return entries;
23
+ }
24
+ export async function showHooks(exec, options = {}) {
25
+ const command = ["tmux", "show-hooks", ...(options.global ? ["-g"] : [])];
26
+ const execution = await execute(exec, command);
27
+ if (execution.exitCode !== 0) {
28
+ throw CommandError.from(command, execution);
29
+ }
30
+ return parseHooks(execution.stdout);
31
+ }
32
+ export async function setHook(exec, options) {
33
+ assertNotEmpty("name", options.name);
34
+ assertNotEmpty("command", options.command);
35
+ const command = [
36
+ "tmux",
37
+ "set-hook",
38
+ ...hookFlags(options.global, options.append),
39
+ options.name,
40
+ options.command,
41
+ ];
42
+ const execution = await execute(exec, command);
43
+ if (execution.exitCode !== 0) {
44
+ throw CommandError.from(command, execution);
45
+ }
46
+ }
47
+ export async function unsetHook(exec, options) {
48
+ assertNotEmpty("name", options.name);
49
+ const command = ["tmux", "set-hook", options.global ? "-gu" : "-u", options.name];
50
+ const execution = await execute(exec, command);
51
+ if (execution.exitCode !== 0) {
52
+ throw CommandError.from(command, execution);
53
+ }
54
+ }
55
+ function hookFlags(global, append) {
56
+ const flags = `${global ? "g" : ""}${append ? "a" : ""}`;
57
+ return flags.length === 0 ? [] : [`-${flags}`];
58
+ }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export type { WindowPaneIds } from "./command.js";
2
2
  export type { CreateTmuxOptions } from "./create-tmux.js";
3
3
  export { createTmux } from "./create-tmux.js";
4
+ export type { HookEntry, SetHookOptions, ShowHooksOptions, UnsetHookOptions } from "./hook.js";
5
+ export { parseHooks, setHook, showHooks, unsetHook } from "./hook.js";
4
6
  export type { RespawnPaneOptions, SplitPaneOptions } from "./pane.js";
5
7
  export { respawnPane } from "./pane.js";
6
8
  export type { Exec, ExecOptions, ExecResult } from "./process.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { createTmux } from "./create-tmux.js";
2
+ export { parseHooks, setHook, showHooks, unsetHook } from "./hook.js";
2
3
  export { respawnPane } from "./pane.js";
3
4
  export { CommandError } from "./process.js";
4
5
  export { killSession, setEnvironment } from "./session.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@termwire/tmux",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",