@termwire/cli 0.4.1 → 0.6.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 +166 -0
- package/README.md +145 -8
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +54 -0
- package/dist/bin/termwire.js +2 -2
- package/dist/config-loader.d.ts +9 -0
- package/dist/config-loader.js +6 -3
- package/dist/config-writer.d.ts +17 -0
- package/dist/config-writer.js +46 -0
- package/dist/down.d.ts +40 -0
- package/dist/down.js +99 -0
- package/dist/exec.d.ts +10 -2
- package/dist/exec.js +7 -4
- package/dist/index.d.ts +7 -17
- package/dist/index.js +2 -9
- package/dist/install-prompt.d.ts +10 -0
- package/dist/install-prompt.js +100 -0
- package/dist/install.d.ts +92 -0
- package/dist/install.js +363 -0
- package/dist/layout-preview.d.ts +20 -0
- package/dist/layout-preview.js +38 -0
- package/dist/process-scan-darwin.d.ts +24 -0
- package/dist/process-scan-darwin.js +102 -0
- package/dist/process-scan-linux.d.ts +13 -0
- package/dist/process-scan-linux.js +66 -0
- package/dist/process-scan.d.ts +37 -23
- package/dist/process-scan.js +30 -131
- package/dist/program.d.ts +21 -50
- package/dist/program.js +199 -144
- package/dist/runtime.d.ts +84 -0
- package/dist/runtime.js +131 -0
- package/dist/skill.d.ts +13 -0
- package/dist/skill.js +17 -0
- package/dist/templates.d.ts +13 -0
- package/dist/templates.js +168 -0
- package/dist/workspaces.d.ts +45 -0
- package/dist/workspaces.js +116 -0
- package/dist/worktree.d.ts +15 -0
- package/dist/worktree.js +65 -7
- package/package.json +6 -3
- package/skills/termwire-open/SKILL.md +27 -0
package/dist/process-scan.d.ts
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* What a process scan is, independent of how a platform performs one.
|
|
3
|
+
*
|
|
4
|
+
* A platform module exports a factory that takes only its own dependencies and
|
|
5
|
+
* returns a `SessionScanner`; `runtime.ts` picks one. Adding a platform is a new
|
|
6
|
+
* module beside this one plus a branch there, and nothing else changes: `reap` and
|
|
7
|
+
* `ls` depend on the type, never on a platform.
|
|
8
|
+
*
|
|
9
|
+
* A new module has to honour five things, and the first two have teeth:
|
|
10
|
+
*
|
|
11
|
+
* 1. `processes` lists **every** process visible to this user, labeled or not. The
|
|
12
|
+
* reap protects itself, the tmux server and their ancestors with it, so a partial
|
|
13
|
+
* list is how a reap comes to signal its own parent.
|
|
14
|
+
* 2. A listing that fails throws. Returning an empty scan reads as "nothing to kill",
|
|
15
|
+
* and the reap would log a clean sweep while the processes it exists to kill keep
|
|
16
|
+
* running.
|
|
17
|
+
* 3. A pid counts as labeled only when `TERMWIRE_SESSION=<session>` is in its
|
|
18
|
+
* environment. A command line that merely mentions the label is not a match.
|
|
19
|
+
* 4. `labeled` carries a key for every requested session, empty list included, which
|
|
20
|
+
* `emptyLabels` is for.
|
|
21
|
+
* 5. `rss` is in KiB, and 0 when the platform cannot say.
|
|
22
|
+
*/
|
|
2
23
|
export declare const sessionVariable = "TERMWIRE_SESSION";
|
|
3
24
|
export interface ProcessEntry {
|
|
4
25
|
pid: number;
|
|
5
26
|
ppid: number;
|
|
27
|
+
/** Resident set size in KiB, the unit `ps -o rss=` already reports. */
|
|
28
|
+
rss: number;
|
|
6
29
|
}
|
|
7
30
|
export interface ProcessScan {
|
|
8
31
|
/** Every process visible to the current user, used to protect our own ancestors. */
|
|
@@ -10,28 +33,19 @@ export interface ProcessScan {
|
|
|
10
33
|
/** Processes whose environment carries the scanned session label. */
|
|
11
34
|
labeled: number[];
|
|
12
35
|
}
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
export interface LinuxScanDependencies {
|
|
19
|
-
readdir: (path: string) => Promise<string[]>;
|
|
20
|
-
readFile: (path: string) => Promise<string>;
|
|
21
|
-
}
|
|
22
|
-
export interface ProcessScannerDependencies extends DarwinScanDependencies, LinuxScanDependencies {
|
|
23
|
-
platform: string;
|
|
24
|
-
}
|
|
25
|
-
export interface DarwinProcessRow extends ProcessEntry {
|
|
26
|
-
/** The command line, followed by the environment when `ps -E` could read it. */
|
|
27
|
-
details: string;
|
|
36
|
+
export interface SessionScan {
|
|
37
|
+
processes: ProcessEntry[];
|
|
38
|
+
/** The labeled pids of each requested session, that session's own key. */
|
|
39
|
+
labeled: Map<string, number[]>;
|
|
28
40
|
}
|
|
29
|
-
export
|
|
30
|
-
export declare function parseDarwinProcesses(stdout: string): DarwinProcessRow[];
|
|
31
|
-
export declare function parseDarwinArguments(stdout: string): Map<number, string>;
|
|
32
|
-
export declare function parseLinuxPpid(stat: string): number | undefined;
|
|
41
|
+
export type ProcessScanner = (session: string) => Promise<ProcessScan>;
|
|
33
42
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
43
|
+
* Several sessions from one walk over the process table. `ls` reads every workspace
|
|
44
|
+
* at once, and a walk per workspace would cost a full process listing each time.
|
|
36
45
|
*/
|
|
37
|
-
export
|
|
46
|
+
export type SessionScanner = (sessions: readonly string[]) => Promise<SessionScan>;
|
|
47
|
+
/** The assignment a labeled process carries, and what a scanner looks for. */
|
|
48
|
+
export declare function sessionLabel(session: string): string;
|
|
49
|
+
export declare function emptyLabels(sessions: readonly string[]): Map<string, number[]>;
|
|
50
|
+
/** The reap works one session at a time, and asks a scanner for exactly that. */
|
|
51
|
+
export declare function toSingleSession(scan: SessionScanner): ProcessScanner;
|
package/dist/process-scan.js
CHANGED
|
@@ -1,138 +1,37 @@
|
|
|
1
|
-
export const sessionVariable = "TERMWIRE_SESSION";
|
|
2
|
-
export function createProcessScanner(dependencies) {
|
|
3
|
-
if (dependencies.platform === "darwin") {
|
|
4
|
-
return createDarwinScanner(dependencies);
|
|
5
|
-
}
|
|
6
|
-
if (dependencies.platform === "linux") {
|
|
7
|
-
return createLinuxScanner(dependencies);
|
|
8
|
-
}
|
|
9
|
-
throw new Error(`process scanning is not supported on ${dependencies.platform}`);
|
|
10
|
-
}
|
|
11
|
-
export function parseDarwinProcesses(stdout) {
|
|
12
|
-
const rows = [];
|
|
13
|
-
for (const line of stdout.split("\n")) {
|
|
14
|
-
const parsed = /^\s*(\d+)\s+(\d+)\s(.*)$/.exec(line);
|
|
15
|
-
if (!parsed)
|
|
16
|
-
continue;
|
|
17
|
-
rows.push({ pid: Number(parsed[1]), ppid: Number(parsed[2]), details: parsed[3] });
|
|
18
|
-
}
|
|
19
|
-
return rows;
|
|
20
|
-
}
|
|
21
|
-
export function parseDarwinArguments(stdout) {
|
|
22
|
-
const args = new Map();
|
|
23
|
-
for (const line of stdout.split("\n")) {
|
|
24
|
-
const parsed = /^\s*(\d+)\s(.*)$/.exec(line);
|
|
25
|
-
if (!parsed)
|
|
26
|
-
continue;
|
|
27
|
-
args.set(Number(parsed[1]), parsed[2]);
|
|
28
|
-
}
|
|
29
|
-
return args;
|
|
30
|
-
}
|
|
31
|
-
export function parseLinuxPpid(stat) {
|
|
32
|
-
const commandEnd = stat.lastIndexOf(")");
|
|
33
|
-
if (commandEnd === -1)
|
|
34
|
-
return undefined;
|
|
35
|
-
const fields = stat
|
|
36
|
-
.slice(commandEnd + 1)
|
|
37
|
-
.trim()
|
|
38
|
-
.split(/\s+/);
|
|
39
|
-
const ppid = Number(fields[1]);
|
|
40
|
-
return Number.isInteger(ppid) ? ppid : undefined;
|
|
41
|
-
}
|
|
42
1
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
2
|
+
* What a process scan is, independent of how a platform performs one.
|
|
3
|
+
*
|
|
4
|
+
* A platform module exports a factory that takes only its own dependencies and
|
|
5
|
+
* returns a `SessionScanner`; `runtime.ts` picks one. Adding a platform is a new
|
|
6
|
+
* module beside this one plus a branch there, and nothing else changes: `reap` and
|
|
7
|
+
* `ls` depend on the type, never on a platform.
|
|
8
|
+
*
|
|
9
|
+
* A new module has to honour five things, and the first two have teeth:
|
|
10
|
+
*
|
|
11
|
+
* 1. `processes` lists **every** process visible to this user, labeled or not. The
|
|
12
|
+
* reap protects itself, the tmux server and their ancestors with it, so a partial
|
|
13
|
+
* list is how a reap comes to signal its own parent.
|
|
14
|
+
* 2. A listing that fails throws. Returning an empty scan reads as "nothing to kill",
|
|
15
|
+
* and the reap would log a clean sweep while the processes it exists to kill keep
|
|
16
|
+
* running.
|
|
17
|
+
* 3. A pid counts as labeled only when `TERMWIRE_SESSION=<session>` is in its
|
|
18
|
+
* environment. A command line that merely mentions the label is not a match.
|
|
19
|
+
* 4. `labeled` carries a key for every requested session, empty list included, which
|
|
20
|
+
* `emptyLabels` is for.
|
|
21
|
+
* 5. `rss` is in KiB, and 0 when the platform cannot say.
|
|
45
22
|
*/
|
|
46
|
-
export
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const after = index + needle.length >= text.length ? " " : text.charAt(index + needle.length);
|
|
51
|
-
if (/\s/.test(before) && /\s/.test(after))
|
|
52
|
-
return true;
|
|
53
|
-
index = text.indexOf(needle, index + 1);
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
23
|
+
export const sessionVariable = "TERMWIRE_SESSION";
|
|
24
|
+
/** The assignment a labeled process carries, and what a scanner looks for. */
|
|
25
|
+
export function sessionLabel(session) {
|
|
26
|
+
return `${sessionVariable}=${session}`;
|
|
56
27
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
* the command column. A process that only mentions the label on its command line
|
|
60
|
-
* would look identical, so every candidate is re-read without `-E` and the command
|
|
61
|
-
* line is stripped off before the label is looked up in what remains.
|
|
62
|
-
*/
|
|
63
|
-
function createDarwinScanner(dependencies) {
|
|
64
|
-
return async (session) => {
|
|
65
|
-
const needle = `${sessionVariable}=${session}`;
|
|
66
|
-
const rows = parseDarwinProcesses(await runPs(dependencies.exec, [
|
|
67
|
-
"ps",
|
|
68
|
-
"-Eww",
|
|
69
|
-
"-o",
|
|
70
|
-
"pid=,ppid=,command=",
|
|
71
|
-
"-U",
|
|
72
|
-
String(dependencies.uid),
|
|
73
|
-
]));
|
|
74
|
-
const processes = rows.map(({ pid, ppid }) => ({ pid, ppid }));
|
|
75
|
-
const candidates = rows.filter((row) => containsAssignment(row.details, needle));
|
|
76
|
-
if (candidates.length === 0) {
|
|
77
|
-
return { processes, labeled: [] };
|
|
78
|
-
}
|
|
79
|
-
const args = parseDarwinArguments(await runPs(dependencies.exec, ["ps", "-ww", "-o", "pid=,args=", "-p", candidates.map((c) => c.pid).join(",")],
|
|
80
|
-
// `ps` exits non-zero when none of the requested pids exist any more, and a
|
|
81
|
-
// candidate that just died is simply not a target.
|
|
82
|
-
{ allowMissingPids: true }));
|
|
83
|
-
const labeled = candidates
|
|
84
|
-
.filter((row) => {
|
|
85
|
-
const argv = args.get(row.pid);
|
|
86
|
-
if (argv === undefined || !row.details.startsWith(argv))
|
|
87
|
-
return false;
|
|
88
|
-
return containsAssignment(row.details.slice(argv.length), needle);
|
|
89
|
-
})
|
|
90
|
-
.map((row) => row.pid);
|
|
91
|
-
return { processes, labeled };
|
|
92
|
-
};
|
|
28
|
+
export function emptyLabels(sessions) {
|
|
29
|
+
return new Map(sessions.map((session) => [session, []]));
|
|
93
30
|
}
|
|
94
|
-
|
|
31
|
+
/** The reap works one session at a time, and asks a scanner for exactly that. */
|
|
32
|
+
export function toSingleSession(scan) {
|
|
95
33
|
return async (session) => {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
const labeled = [];
|
|
99
|
-
for (const entry of await dependencies.readdir("/proc")) {
|
|
100
|
-
if (!/^\d+$/.test(entry))
|
|
101
|
-
continue;
|
|
102
|
-
const pid = Number(entry);
|
|
103
|
-
const stat = await readOptional(dependencies, `/proc/${entry}/stat`);
|
|
104
|
-
if (stat === undefined)
|
|
105
|
-
continue;
|
|
106
|
-
const ppid = parseLinuxPpid(stat);
|
|
107
|
-
if (ppid === undefined)
|
|
108
|
-
continue;
|
|
109
|
-
processes.push({ pid, ppid });
|
|
110
|
-
// Readable only for our own processes, so other users are skipped silently.
|
|
111
|
-
const environ = await readOptional(dependencies, `/proc/${entry}/environ`);
|
|
112
|
-
if (environ === undefined)
|
|
113
|
-
continue;
|
|
114
|
-
if (environ.split("\0").includes(label))
|
|
115
|
-
labeled.push(pid);
|
|
116
|
-
}
|
|
117
|
-
return { processes, labeled };
|
|
34
|
+
const { processes, labeled } = await scan([session]);
|
|
35
|
+
return { processes, labeled: labeled.get(session) ?? [] };
|
|
118
36
|
};
|
|
119
37
|
}
|
|
120
|
-
async function runPs(exec, argv, options = {}) {
|
|
121
|
-
const execution = await exec(argv);
|
|
122
|
-
if (execution.exitCode === 0)
|
|
123
|
-
return execution.stdout;
|
|
124
|
-
if (options.allowMissingPids && execution.stdout.trim().length === 0)
|
|
125
|
-
return "";
|
|
126
|
-
// A failed listing must not read as an empty one: that would log a clean reap
|
|
127
|
-
// while the processes it exists to kill keep running.
|
|
128
|
-
const details = execution.stderr.trim();
|
|
129
|
-
throw new Error(`${argv.join(" ")} failed (exit ${execution.exitCode})${details ? `: ${details}` : ""}`);
|
|
130
|
-
}
|
|
131
|
-
async function readOptional(dependencies, path) {
|
|
132
|
-
try {
|
|
133
|
-
return await dependencies.readFile(path);
|
|
134
|
-
}
|
|
135
|
-
catch {
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
}
|
package/dist/program.d.ts
CHANGED
|
@@ -1,62 +1,33 @@
|
|
|
1
|
-
import { createNvim } from "@termwire/nvim";
|
|
2
|
-
import { createTmux } from "@termwire/tmux";
|
|
3
1
|
import { Command } from "commander";
|
|
4
|
-
import { type
|
|
2
|
+
import { type DownRequest, type DownResult } from "./down.js";
|
|
3
|
+
import { type InstallRequest, type InstallResult } from "./install.js";
|
|
5
4
|
import { type OpenRequest, type OpenResult } from "./open.js";
|
|
6
|
-
import
|
|
5
|
+
import type { CliRuntime } from "./runtime.js";
|
|
7
6
|
import { type UpRequest } from "./up.js";
|
|
8
|
-
import { type
|
|
7
|
+
import { type Workspace } from "./workspaces.js";
|
|
9
8
|
export interface ProgramDependencies {
|
|
10
9
|
up: (request: UpRequest) => Promise<void>;
|
|
10
|
+
ls: () => Promise<Workspace[]>;
|
|
11
|
+
down: (request: DownRequest) => Promise<DownResult>;
|
|
11
12
|
open: (request: OpenRequest) => Promise<OpenResult>;
|
|
12
13
|
reap: (session: string) => Promise<void>;
|
|
14
|
+
install: (request: InstallRequest) => Promise<InstallResult>;
|
|
15
|
+
/** Only the table shortens `$HOME` to `~`; the JSON rows keep absolute paths. */
|
|
16
|
+
homedir: () => string;
|
|
13
17
|
writeError: (message: string) => void;
|
|
14
18
|
writeOutput: (message: string) => void;
|
|
15
19
|
}
|
|
16
|
-
export interface RuntimeDependencies {
|
|
17
|
-
createTmux: () => ReturnType<typeof createTmux>;
|
|
18
|
-
cwd: () => string;
|
|
19
|
-
env: Record<string, string | undefined>;
|
|
20
|
-
homedir: () => string;
|
|
21
|
-
execPath: string;
|
|
22
|
-
scriptPath: string | undefined;
|
|
23
|
-
warn: (message: string) => void;
|
|
24
|
-
readFile: (path: string, encoding: "utf8") => Promise<string>;
|
|
25
|
-
gitExec: GitExec;
|
|
26
|
-
mkdir: (path: string, options: {
|
|
27
|
-
recursive: true;
|
|
28
|
-
}) => Promise<unknown>;
|
|
29
|
-
pathExists: (path: string) => Promise<boolean>;
|
|
30
|
-
unlink: (path: string) => Promise<void>;
|
|
31
|
-
}
|
|
32
|
-
export interface RuntimeReapDependencies {
|
|
33
|
-
env: Record<string, string | undefined>;
|
|
34
|
-
homedir: () => string;
|
|
35
|
-
platform: string;
|
|
36
|
-
uid: number;
|
|
37
|
-
pid: number;
|
|
38
|
-
exec: Exec;
|
|
39
|
-
readdir: (path: string) => Promise<string[]>;
|
|
40
|
-
readFile: (path: string) => Promise<string>;
|
|
41
|
-
kill: (pid: number, signal: ReapSignal) => KillOutcome;
|
|
42
|
-
wait: (milliseconds: number) => Promise<void>;
|
|
43
|
-
appendFile: (path: string, contents: string) => Promise<void>;
|
|
44
|
-
mkdir: (path: string, options: {
|
|
45
|
-
recursive: true;
|
|
46
|
-
}) => Promise<unknown>;
|
|
47
|
-
now: () => Date;
|
|
48
|
-
}
|
|
49
|
-
export interface RuntimeOpenDependencies {
|
|
50
|
-
createNvim: () => ReturnType<typeof createNvim>;
|
|
51
|
-
createTmux: () => ReturnType<typeof createTmux>;
|
|
52
|
-
cwd: () => string;
|
|
53
|
-
env: Record<string, string | undefined>;
|
|
54
|
-
}
|
|
55
|
-
export declare const executeGit: GitExec;
|
|
56
20
|
export declare function removeStaleSocket(path: string, unlink: (path: string) => Promise<void>): Promise<void>;
|
|
57
|
-
export declare function
|
|
58
|
-
export declare function
|
|
59
|
-
export declare function
|
|
60
|
-
export declare function
|
|
21
|
+
export declare function createUp({ fs, host, git, tmux, }: CliRuntime): (request: UpRequest) => Promise<void>;
|
|
22
|
+
export declare function createLs({ fs, git, tmux, createScanner }: CliRuntime): () => Promise<Workspace[]>;
|
|
23
|
+
export declare function createDown({ host, git, tmux, }: CliRuntime): (request: DownRequest) => Promise<DownResult>;
|
|
24
|
+
export declare function createOpen({ host, nvim, tmux, }: CliRuntime): (request: OpenRequest) => Promise<OpenResult>;
|
|
25
|
+
export declare function createReap({ fs, host, createScanner, }: CliRuntime): (session: string) => Promise<void>;
|
|
26
|
+
export declare function createInstall({ fs, host, git, createPrompter, }: CliRuntime): (request: InstallRequest) => Promise<InstallResult>;
|
|
27
|
+
/** Binds every command to one runtime. The only caller that needs the real one. */
|
|
28
|
+
export declare function createCommands(runtime: CliRuntime): ProgramDependencies;
|
|
61
29
|
export declare function createProgram(dependencies: ProgramDependencies): Command;
|
|
62
|
-
|
|
30
|
+
/** The composition root's entry: one runtime in, an exit code out. */
|
|
31
|
+
export declare function run(argv: readonly string[], runtime: CliRuntime): Promise<number>;
|
|
32
|
+
/** Parsing and exit-code mapping, indifferent to how the commands were built. */
|
|
33
|
+
export declare function runCommands(argv: readonly string[], commands: ProgramDependencies): Promise<number>;
|