worktree-compose 0.2.0 → 1.0.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/README.md +458 -140
- package/SKILL.md +113 -0
- package/dist/cli.js +94 -33
- package/dist/commands/clean.d.ts +2 -1
- package/dist/commands/clean.js +8 -48
- package/dist/commands/doctor.d.ts +3 -0
- package/dist/commands/doctor.js +30 -0
- package/dist/commands/list.d.ts +4 -1
- package/dist/commands/list.js +62 -44
- package/dist/commands/promote.d.ts +3 -1
- package/dist/commands/promote.js +4 -34
- package/dist/commands/prune.d.ts +2 -0
- package/dist/commands/prune.js +7 -0
- package/dist/commands/restart.d.ts +2 -1
- package/dist/commands/restart.js +11 -5
- package/dist/commands/skill.d.ts +4 -0
- package/dist/commands/skill.js +10 -0
- package/dist/commands/start.d.ts +2 -1
- package/dist/commands/start.js +7 -34
- package/dist/commands/stop.d.ts +1 -1
- package/dist/commands/stop.js +6 -23
- package/dist/compose/detect.d.ts +1 -2
- package/dist/compose/detect.js +1 -10
- package/dist/compose/engine.d.ts +61 -0
- package/dist/compose/engine.js +129 -0
- package/dist/compose/isolation.d.ts +15 -0
- package/dist/compose/isolation.js +62 -0
- package/dist/compose/parse.d.ts +5 -0
- package/dist/compose/parse.js +89 -11
- package/dist/compose/status.d.ts +84 -0
- package/dist/compose/status.js +227 -0
- package/dist/compose/types.d.ts +20 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.js +23 -6
- package/dist/context.d.ts +89 -3
- package/dist/context.js +278 -45
- package/dist/core/clean.d.ts +91 -0
- package/dist/core/clean.js +343 -0
- package/dist/core/compose.d.ts +30 -0
- package/dist/core/compose.js +45 -0
- package/dist/core/doctor.d.ts +28 -0
- package/dist/core/doctor.js +277 -0
- package/dist/core/list.d.ts +42 -0
- package/dist/core/list.js +94 -0
- package/dist/core/promote.d.ts +40 -0
- package/dist/core/promote.js +117 -0
- package/dist/core/prune.d.ts +32 -0
- package/dist/core/prune.js +75 -0
- package/dist/core/restart.d.ts +14 -0
- package/dist/core/restart.js +18 -0
- package/dist/core/start.d.ts +31 -0
- package/dist/core/start.js +99 -0
- package/dist/core/stop.d.ts +32 -0
- package/dist/core/stop.js +57 -0
- package/dist/core/teardown.d.ts +121 -0
- package/dist/core/teardown.js +298 -0
- package/dist/core/types.d.ts +15 -0
- package/dist/core/types.js +1 -0
- package/dist/git/promote.d.ts +49 -2
- package/dist/git/promote.js +160 -38
- package/dist/git/repo.d.ts +72 -0
- package/dist/git/repo.js +167 -0
- package/dist/git/status.d.ts +8 -0
- package/dist/git/status.js +25 -0
- package/dist/git/worktree.d.ts +63 -3
- package/dist/git/worktree.js +173 -29
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.js +230 -115
- package/dist/ports/allocate.d.ts +14 -3
- package/dist/ports/allocate.js +20 -4
- package/dist/selectors.d.ts +10 -0
- package/dist/selectors.js +106 -0
- package/dist/skill.d.ts +13 -0
- package/dist/skill.js +28 -0
- package/dist/state/registry.d.ts +194 -0
- package/dist/state/registry.js +595 -0
- package/dist/sync/env.d.ts +77 -2
- package/dist/sync/env.js +151 -32
- package/dist/sync/files.d.ts +15 -1
- package/dist/sync/files.js +126 -38
- package/dist/utils/exec.d.ts +69 -4
- package/dist/utils/exec.js +221 -15
- package/dist/utils/log.d.ts +37 -0
- package/dist/utils/log.js +68 -4
- package/dist/utils/paths.d.ts +61 -0
- package/dist/utils/paths.js +163 -0
- package/dist/utils/sanitize.d.ts +7 -0
- package/dist/utils/sanitize.js +12 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/workspaces/resolve.d.ts +21 -0
- package/dist/workspaces/resolve.js +235 -0
- package/package.json +18 -14
- package/wtc.schema.json +62 -0
package/dist/git/worktree.js
CHANGED
|
@@ -1,40 +1,184 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { run, runSafe } from "../utils/exec.js";
|
|
4
|
+
/**
|
|
5
|
+
* Parse `git worktree list --porcelain`. Blocks are separated by blank lines;
|
|
6
|
+
* flag lines (`bare`, `detached`, `locked [reason]`, `prunable [reason]`) may
|
|
7
|
+
* carry a trailing reason.
|
|
8
|
+
*/
|
|
9
|
+
export function parseWorktreePorcelain(output) {
|
|
5
10
|
const worktrees = [];
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
11
|
+
let current = null;
|
|
12
|
+
const flush = () => {
|
|
13
|
+
if (current)
|
|
14
|
+
worktrees.push(current);
|
|
15
|
+
current = null;
|
|
16
|
+
};
|
|
17
|
+
for (const line of output.split("\n")) {
|
|
18
|
+
if (line === "") {
|
|
19
|
+
flush();
|
|
20
|
+
continue;
|
|
17
21
|
}
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
if (line.startsWith("worktree ")) {
|
|
23
|
+
flush();
|
|
24
|
+
current = {
|
|
25
|
+
path: path.resolve(line.slice("worktree ".length)),
|
|
26
|
+
branch: "detached",
|
|
22
27
|
isMain: worktrees.length === 0,
|
|
23
|
-
|
|
28
|
+
bare: false,
|
|
29
|
+
detached: false,
|
|
30
|
+
locked: false,
|
|
31
|
+
prunable: false,
|
|
32
|
+
};
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (!current)
|
|
36
|
+
continue;
|
|
37
|
+
const wt = current;
|
|
38
|
+
const [key, ...rest] = line.split(" ");
|
|
39
|
+
const value = rest.join(" ");
|
|
40
|
+
switch (key) {
|
|
41
|
+
case "HEAD":
|
|
42
|
+
wt.head = value;
|
|
43
|
+
break;
|
|
44
|
+
case "branch":
|
|
45
|
+
wt.branch = value.replace(/^refs\/heads\//, "");
|
|
46
|
+
break;
|
|
47
|
+
case "bare":
|
|
48
|
+
wt.bare = true;
|
|
49
|
+
break;
|
|
50
|
+
case "detached":
|
|
51
|
+
wt.detached = true;
|
|
52
|
+
break;
|
|
53
|
+
case "locked":
|
|
54
|
+
wt.locked = true;
|
|
55
|
+
break;
|
|
56
|
+
case "prunable":
|
|
57
|
+
wt.prunable = true;
|
|
58
|
+
break;
|
|
24
59
|
}
|
|
25
60
|
}
|
|
61
|
+
flush();
|
|
26
62
|
return worktrees;
|
|
27
63
|
}
|
|
28
|
-
|
|
29
|
-
|
|
64
|
+
/** True when two paths name the same directory (resolving symlinks when they exist). */
|
|
65
|
+
export function samePath(a, b) {
|
|
66
|
+
if (path.resolve(a) === path.resolve(b))
|
|
67
|
+
return true;
|
|
68
|
+
try {
|
|
69
|
+
return fs.realpathSync.native(a) === fs.realpathSync.native(b);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
30
74
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
75
|
+
/**
|
|
76
|
+
* A linked worktree's git admin dir (`<common dir>/worktrees/<name>`):
|
|
77
|
+
* read from its `.git` file (`gitdir: …`, relative to the worktree) with no
|
|
78
|
+
* subprocess, else asked of git. `undefined` when neither answers.
|
|
79
|
+
*/
|
|
80
|
+
export function worktreeAdminDir(wtPath) {
|
|
81
|
+
try {
|
|
82
|
+
const match = /^gitdir:\s*(.+?)\s*$/m.exec(fs.readFileSync(path.join(wtPath, ".git"), "utf-8"));
|
|
83
|
+
if (match)
|
|
84
|
+
return path.resolve(wtPath, match[1]);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// Not a readable file: ask git below.
|
|
88
|
+
}
|
|
89
|
+
const dir = runSafe("git", ["-C", wtPath, "rev-parse", "--absolute-git-dir"]);
|
|
90
|
+
return dir ? path.resolve(dir) : undefined;
|
|
34
91
|
}
|
|
35
|
-
|
|
36
|
-
|
|
92
|
+
/**
|
|
93
|
+
* The admin dir (`<common dir>/worktrees/<name>`) whose `gitdir` file points
|
|
94
|
+
* at `<wtPath>/.git` — found without the checkout, which may be missing.
|
|
95
|
+
*/
|
|
96
|
+
export function adminDirByPath(commonDir, wtPath) {
|
|
97
|
+
const dir = path.join(commonDir, "worktrees");
|
|
98
|
+
let names;
|
|
99
|
+
try {
|
|
100
|
+
names = fs.readdirSync(dir);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
const target = path.join(path.resolve(wtPath), ".git");
|
|
106
|
+
for (const name of names) {
|
|
107
|
+
const admin = path.join(dir, name);
|
|
108
|
+
let gitdir;
|
|
109
|
+
try {
|
|
110
|
+
gitdir = fs.readFileSync(path.join(admin, "gitdir"), "utf-8").trim();
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// Absolute, or relative to the admin dir (worktree.useRelativePaths).
|
|
116
|
+
if (gitdir && path.resolve(admin, gitdir) === target)
|
|
117
|
+
return admin;
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check a linked worktree's checkout on disk: find its admin dir, and flag
|
|
123
|
+
* it when the checkout is gone. Git only reports `prunable` since 2.31, and
|
|
124
|
+
* never for locked worktrees — without this a deleted worktree counted as
|
|
125
|
+
* live and `start` recreated its directory (holding nothing but
|
|
126
|
+
* compose.yaml and .env) to run compose in. A locked one whose checkout is
|
|
127
|
+
* gone (an unmounted drive) is `missing`, not removed: git keeps it, and so
|
|
128
|
+
* does wtc. Its lock is also read from the admin dir, for a git whose
|
|
129
|
+
* porcelain does not report it.
|
|
130
|
+
*/
|
|
131
|
+
function inspectCheckout(wt, commonDir) {
|
|
132
|
+
if (wt.isMain || wt.bare || wt.prunable)
|
|
133
|
+
return wt;
|
|
134
|
+
if (!fs.lstatSync(path.join(wt.path, ".git"), { throwIfNoEntry: false })) {
|
|
135
|
+
const common = commonDir();
|
|
136
|
+
const adminDir = common ? adminDirByPath(common, wt.path) : undefined;
|
|
137
|
+
const locked = wt.locked || (adminDir !== undefined && fs.existsSync(path.join(adminDir, "locked")));
|
|
138
|
+
if (!locked)
|
|
139
|
+
return { ...wt, prunable: true };
|
|
140
|
+
return adminDir ? { ...wt, locked: true, missing: true, adminDir } : { ...wt, locked: true, missing: true };
|
|
141
|
+
}
|
|
142
|
+
const adminDir = worktreeAdminDir(wt.path);
|
|
143
|
+
return adminDir ? { ...wt, adminDir } : wt;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Every worktree git knows about, main first, with on-disk checks applied.
|
|
147
|
+
* `mainRoot` (from getRepoInfo, when it found the main checkout) is marked
|
|
148
|
+
* main too: in a bare-repo layout the first entry is the bare repo and the
|
|
149
|
+
* main checkout is one of the linked worktrees — it must never be started,
|
|
150
|
+
* synced onto, or removed. Pass no `mainRoot` when the main checkout is
|
|
151
|
+
* unknown: then no linked worktree is mistaken for it.
|
|
152
|
+
*/
|
|
153
|
+
export function getWorktrees(repoRoot, mainRoot, commonDir) {
|
|
154
|
+
let common = commonDir;
|
|
155
|
+
const commonDirOf = () => {
|
|
156
|
+
if (common === undefined) {
|
|
157
|
+
// git < 2.31 has no --path-format: resolve the relative answer instead.
|
|
158
|
+
const abs = runSafe("git", ["-C", repoRoot, "rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
159
|
+
const out = abs && path.isAbsolute(abs) ? abs : runSafe("git", ["-C", repoRoot, "rev-parse", "--git-common-dir"]);
|
|
160
|
+
common = out ? path.resolve(repoRoot, out) : "";
|
|
161
|
+
}
|
|
162
|
+
return common || undefined;
|
|
163
|
+
};
|
|
164
|
+
return parseWorktreePorcelain(run("git", ["-C", repoRoot, "worktree", "list", "--porcelain"])).map((wt, i) => {
|
|
165
|
+
// The first entry is the main checkout, the bare repo, or a git dir.
|
|
166
|
+
if (i === 0 || wt.bare)
|
|
167
|
+
return wt;
|
|
168
|
+
const inspected = inspectCheckout(wt, commonDirOf);
|
|
169
|
+
return mainRoot !== undefined && samePath(inspected.path, mainRoot)
|
|
170
|
+
? { ...inspected, isMain: true, markedMain: true }
|
|
171
|
+
: inspected;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/** Linked worktrees wtc can manage: not the main one, not bare, still on disk. */
|
|
175
|
+
export function isCandidate(wt) {
|
|
176
|
+
return !wt.isMain && !wt.bare && !wt.prunable && !wt.missing;
|
|
37
177
|
}
|
|
38
|
-
export function
|
|
39
|
-
return
|
|
178
|
+
export function getNonMainWorktrees(repoRoot) {
|
|
179
|
+
return getWorktrees(repoRoot).filter(isCandidate);
|
|
180
|
+
}
|
|
181
|
+
export function getWorktreeBranch(wtPath) {
|
|
182
|
+
return (runSafe("git", ["-C", wtPath, "rev-parse", "--abbrev-ref", "HEAD"]) ??
|
|
183
|
+
"detached");
|
|
40
184
|
}
|
package/dist/mcp/server.d.ts
CHANGED
package/dist/mcp/server.js
CHANGED
|
@@ -1,131 +1,246 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
2
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const project = composeProjectName(ctx.repoName, idx, wt.branch);
|
|
22
|
-
const allocations = allocateWorktreePorts(ctx.portMappings, idx, ctx.portStride);
|
|
23
|
-
syncWorktreeFiles(ctx.repoRoot, wt.path, ctx.composeFile, ctx.config.sync);
|
|
24
|
-
copyBaseEnv(ctx.repoRoot, wt.path);
|
|
25
|
-
injectPortOverrides(`${wt.path}/.env`, allocations, ctx.config.envOverrides);
|
|
26
|
-
exec(`docker compose -p "${project}" up -d --build`, { cwd: wt.path });
|
|
27
|
-
const ports = allocations.map((a) => `${a.envVar}=${a.port}`).join(", ");
|
|
28
|
-
results.push(`Worktree ${idx} (${wt.branch}): started [${ports}]`);
|
|
29
|
-
}
|
|
30
|
-
return results.join("\n");
|
|
4
|
+
import { startWorktrees } from "../core/start.js";
|
|
5
|
+
import { restartWorktrees } from "../core/restart.js";
|
|
6
|
+
import { stopWorktrees } from "../core/stop.js";
|
|
7
|
+
import { listWorktrees } from "../core/list.js";
|
|
8
|
+
import { promoteWorktree } from "../core/promote.js";
|
|
9
|
+
import { clean } from "../core/clean.js";
|
|
10
|
+
import { prune } from "../core/prune.js";
|
|
11
|
+
import { doctor } from "../core/doctor.js";
|
|
12
|
+
import { throwIfAborted } from "../core/compose.js";
|
|
13
|
+
import { CollectingReporter } from "../utils/log.js";
|
|
14
|
+
import { VERSION } from "../version.js";
|
|
15
|
+
function text(...parts) {
|
|
16
|
+
return {
|
|
17
|
+
content: parts
|
|
18
|
+
.filter((p) => p !== undefined && p !== "")
|
|
19
|
+
.map((p) => ({ type: "text", text: p })),
|
|
20
|
+
};
|
|
31
21
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
22
|
+
const PROGRESS_INTERVAL_MS = 5_000;
|
|
23
|
+
/**
|
|
24
|
+
* Progress notifications for a request that asked for them (progressToken):
|
|
25
|
+
* one per reported line, throttled, plus a heartbeat carrying the latest
|
|
26
|
+
* compose output line while a long `up --build` runs.
|
|
27
|
+
*/
|
|
28
|
+
function progressSender(extra) {
|
|
29
|
+
const token = extra?._meta?.progressToken;
|
|
30
|
+
if (token === undefined || !extra)
|
|
31
|
+
return { note: () => { }, stop: () => { } };
|
|
32
|
+
let count = 0;
|
|
33
|
+
let last = "";
|
|
34
|
+
let lastSent = 0;
|
|
35
|
+
const send = (message) => {
|
|
36
|
+
lastSent = Date.now();
|
|
37
|
+
void extra
|
|
38
|
+
.sendNotification({
|
|
39
|
+
method: "notifications/progress",
|
|
40
|
+
params: { progressToken: token, progress: ++count, message },
|
|
41
|
+
})
|
|
42
|
+
.catch(() => { });
|
|
43
|
+
};
|
|
44
|
+
const timer = setInterval(() => {
|
|
45
|
+
if (Date.now() - lastSent >= PROGRESS_INTERVAL_MS && last)
|
|
46
|
+
send(last);
|
|
47
|
+
}, PROGRESS_INTERVAL_MS);
|
|
48
|
+
timer.unref?.();
|
|
49
|
+
return {
|
|
50
|
+
note: (message) => {
|
|
51
|
+
last = message;
|
|
52
|
+
if (Date.now() - lastSent >= 1_000)
|
|
53
|
+
send(message);
|
|
54
|
+
},
|
|
55
|
+
stop: () => clearInterval(timer),
|
|
56
|
+
};
|
|
50
57
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Serializes the tools that change stacks or files (start, stop, restart,
|
|
60
|
+
* clean, prune, promote). A client that times out and retries `wtc_start`
|
|
61
|
+
* must not launch a second `compose up` for the same project while the
|
|
62
|
+
* first is still building, and a promote must not copy files while a clean
|
|
63
|
+
* removes the worktree: each call waits its turn. A request cancelled while
|
|
64
|
+
* it waits never runs.
|
|
65
|
+
*/
|
|
66
|
+
let mutating = Promise.resolve();
|
|
67
|
+
function exclusive(io, fn) {
|
|
68
|
+
const run = () => {
|
|
69
|
+
throwIfAborted(io);
|
|
70
|
+
return fn();
|
|
71
|
+
};
|
|
72
|
+
const next = mutating.then(run, run);
|
|
73
|
+
mutating = next.catch(() => { });
|
|
74
|
+
return next;
|
|
68
75
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Run an operation with a collecting reporter. Handlers never throw: errors
|
|
78
|
+
* come back as `isError: true` with the progress so far plus the message
|
|
79
|
+
* (which includes compose's stderr tail on compose failures). The request's
|
|
80
|
+
* abort signal cancels running compose commands.
|
|
81
|
+
*/
|
|
82
|
+
async function runTool(extra, fn) {
|
|
83
|
+
const progress = progressSender(extra);
|
|
84
|
+
const reporter = new CollectingReporter((line) => progress.note(line));
|
|
85
|
+
// Compose output is captured, never streamed: stdout is the JSON-RPC channel.
|
|
86
|
+
const io = { live: false, signal: extra?.signal, onOutput: (line) => progress.note(line) };
|
|
87
|
+
try {
|
|
88
|
+
const outcome = (await fn(reporter, io)) ?? {};
|
|
89
|
+
const result = text(reporter.text(), ...(outcome.extra ?? []));
|
|
90
|
+
if (result.content.length === 0)
|
|
91
|
+
result.content.push({ type: "text", text: "Done." });
|
|
92
|
+
return outcome.isError ? { ...result, isError: true } : result;
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
96
|
+
return { ...text(reporter.text(), `Error: ${msg}`), isError: true };
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
progress.stop();
|
|
82
100
|
}
|
|
83
|
-
promoteFiles(repoRoot, wt.path, files);
|
|
84
|
-
return `Promoted ${files.length} file(s) from worktree ${index} (${wt.branch}) into ${currentBranch}:\n${files.join("\n")}`;
|
|
85
101
|
}
|
|
86
|
-
|
|
102
|
+
const worktreesSchema = z
|
|
103
|
+
.array(z.union([z.number().int(), z.string()]))
|
|
104
|
+
.optional()
|
|
105
|
+
.describe("Worktrees to act on: indices, branch names, or directory names (as shown by wtc_list). Omit for all.");
|
|
106
|
+
const envFileSchema = z
|
|
107
|
+
.string()
|
|
108
|
+
.optional()
|
|
109
|
+
.describe('Env filename to copy and inject, e.g. ".env.development". Omit to use the configured "envFile" (.wtcrc.json / package.json#wtc), else ".env".');
|
|
110
|
+
const workspacesSchema = z
|
|
111
|
+
.array(z.string())
|
|
112
|
+
.optional()
|
|
113
|
+
.describe("Monorepo workspaces (by package name, directory, or path) that also get env injected.");
|
|
114
|
+
const buildSchema = z
|
|
115
|
+
.boolean()
|
|
116
|
+
.optional()
|
|
117
|
+
.describe("Rebuild images (docker compose up --build). Default: true.");
|
|
118
|
+
export function createMcpServer() {
|
|
87
119
|
const server = new McpServer({
|
|
88
120
|
name: "worktree-compose",
|
|
89
|
-
version:
|
|
121
|
+
version: VERSION,
|
|
90
122
|
});
|
|
91
|
-
server.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
123
|
+
server.registerTool("wtc_start", {
|
|
124
|
+
title: "Start worktree stacks",
|
|
125
|
+
description: "Start Docker Compose stacks for worktrees. Syncs compose/infra files from the main checkout, injects isolated ports and COMPOSE_PROJECT_NAME into env files, runs compose up -d. Sends progress notifications when the request carries a progressToken; cancelling the request stops compose.",
|
|
126
|
+
inputSchema: {
|
|
127
|
+
indices: worktreesSchema,
|
|
128
|
+
envFile: envFileSchema,
|
|
129
|
+
workspaces: workspacesSchema,
|
|
130
|
+
build: buildSchema,
|
|
131
|
+
},
|
|
132
|
+
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
133
|
+
}, async ({ indices, envFile, workspaces, build }, extra) => runTool(extra, (reporter, io) => exclusive(io, async () => {
|
|
134
|
+
await startWorktrees({ worktrees: indices, envFile, workspaces, build }, reporter, io);
|
|
135
|
+
})));
|
|
136
|
+
server.registerTool("wtc_stop", {
|
|
137
|
+
title: "Stop worktree stacks",
|
|
138
|
+
description: "Stop the containers of worktree stacks (compose stop). Containers, networks and volumes are kept; wtc_start resumes them. To remove a stack and its volumes, use wtc_clean / wtc_prune.",
|
|
139
|
+
inputSchema: { indices: worktreesSchema },
|
|
140
|
+
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
141
|
+
}, async ({ indices }, extra) => runTool(extra, (reporter, io) => exclusive(io, async () => {
|
|
142
|
+
const result = await stopWorktrees({ worktrees: indices }, reporter, io);
|
|
143
|
+
return { isError: result.failures > 0 };
|
|
144
|
+
})));
|
|
145
|
+
server.registerTool("wtc_restart", {
|
|
146
|
+
title: "Restart worktree stacks",
|
|
147
|
+
description: "Restart Docker Compose stacks for worktrees (compose stop, then re-sync files and env and compose up -d, which recreates changed services). Use after DB migrations, Dockerfile changes, or config updates.",
|
|
148
|
+
inputSchema: {
|
|
149
|
+
indices: worktreesSchema,
|
|
150
|
+
envFile: envFileSchema,
|
|
151
|
+
workspaces: workspacesSchema,
|
|
152
|
+
build: buildSchema,
|
|
153
|
+
},
|
|
154
|
+
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
155
|
+
}, async ({ indices, envFile, workspaces, build }, extra) => runTool(extra, (reporter, io) => exclusive(io, async () => {
|
|
156
|
+
await restartWorktrees({ worktrees: indices, envFile, workspaces, build }, reporter, io);
|
|
157
|
+
})));
|
|
158
|
+
server.registerTool("wtc_list", {
|
|
159
|
+
title: "List worktrees",
|
|
160
|
+
description: "List worktrees with index, branch, path, compose project, status (up/partial/exited/down/?), running/total containers, URL, and ports. The first content item is the JSON array (same as `wtc list --json`); warnings follow as separate items. Rows with orphan=true are removed worktrees whose stack or record remains (clean up with wtc_prune); rows with unavailable set (\"missing\": a locked worktree whose directory is missing, e.g. an unmounted drive; \"main\": it became the main checkout) still exist and are left alone by wtc_prune and wtc_clean. Read-only: never writes wtc's registry.",
|
|
161
|
+
inputSchema: {},
|
|
162
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
163
|
+
}, async (_args, extra) => runTool(extra, async () => {
|
|
164
|
+
const result = listWorktrees({ live: false });
|
|
165
|
+
return {
|
|
166
|
+
extra: [
|
|
167
|
+
JSON.stringify(result.rows, null, 2),
|
|
168
|
+
...result.warnings.map((w) => `⚠ ${w}`),
|
|
169
|
+
...(result.isolationWarnings.length > 0
|
|
170
|
+
? [`⚠ ${result.isolationWarnings.length} isolation warning(s) — run wtc_doctor`]
|
|
171
|
+
: []),
|
|
172
|
+
],
|
|
173
|
+
};
|
|
106
174
|
}));
|
|
107
|
-
server.
|
|
108
|
-
|
|
175
|
+
server.registerTool("wtc_promote", {
|
|
176
|
+
title: "Promote worktree changes",
|
|
177
|
+
description: "Copy changed files (committed, staged, unstaged, untracked) from a worktree into the checkout the server runs in, as uncommitted changes; files the worktree deleted are deleted there. Aborts if any of those files has local changes there, or was also changed (committed) on that branch since the worktree branched — unless force is set.",
|
|
178
|
+
inputSchema: {
|
|
179
|
+
index: z
|
|
180
|
+
.union([z.number().int(), z.string()])
|
|
181
|
+
.describe("Worktree to promote: index, branch name, or directory name."),
|
|
182
|
+
force: z
|
|
183
|
+
.boolean()
|
|
184
|
+
.optional()
|
|
185
|
+
.describe("Also overwrite files the target branch changed since the worktree branched (reverting those commits). Default: false."),
|
|
186
|
+
},
|
|
187
|
+
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false },
|
|
188
|
+
}, async ({ index, force }, extra) => runTool(extra, (reporter, io) => exclusive(io, async () => {
|
|
189
|
+
await promoteWorktree({ worktree: index, force }, reporter, io);
|
|
190
|
+
})));
|
|
191
|
+
server.registerTool("wtc_clean", {
|
|
192
|
+
title: "Clean up all worktree stacks",
|
|
193
|
+
description: "Tear down every worktree stack (down -v: deletes their volumes), remove worktrees that hold no work of their own (no uncommitted changes, no commits on a detached HEAD, no unpushed submodule commits; never the current one or the main checkout), remove leftover containers/networks/volumes of stacks this checkout owns, and prune. Stacks merely named like this repo's (e.g. another clone with the same directory name) are listed, never removed. Returns isError when a teardown or removal failed. Use dryRun first to preview.",
|
|
194
|
+
inputSchema: {
|
|
195
|
+
force: z
|
|
196
|
+
.boolean()
|
|
197
|
+
.optional()
|
|
198
|
+
.describe("Also remove worktrees with uncommitted or unreachable work, and locked ones. Default: false."),
|
|
199
|
+
keepWorktrees: z
|
|
200
|
+
.boolean()
|
|
201
|
+
.optional()
|
|
202
|
+
.describe("Tear down stacks (deleting their volumes) but keep every worktree. Default: false."),
|
|
203
|
+
dryRun: z
|
|
204
|
+
.boolean()
|
|
205
|
+
.optional()
|
|
206
|
+
.describe("Only report what would happen; changes nothing (not even wtc's registry). Default: false."),
|
|
207
|
+
sweep: z
|
|
208
|
+
.boolean()
|
|
209
|
+
.optional()
|
|
210
|
+
.describe("Also remove <repo>-wt-N-* stacks whose worktree directory no longer exists (e.g. left by wtc 0.2), judged from their containers: volumes or networks with no container left are listed, never removed. Never a stack whose directory exists. Default: false."),
|
|
211
|
+
},
|
|
212
|
+
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false },
|
|
213
|
+
}, async ({ force, keepWorktrees, dryRun, sweep }, extra) => runTool(extra, (reporter, io) => exclusive(io, async () => {
|
|
214
|
+
const result = await clean({ force, keepWorktrees, dryRun, sweep }, reporter, io);
|
|
215
|
+
return { isError: result.failures > 0 };
|
|
216
|
+
})));
|
|
217
|
+
server.registerTool("wtc_prune", {
|
|
218
|
+
title: "Prune removed worktrees",
|
|
219
|
+
description: "Tear down (down -v) and forget the stacks of worktrees that were deleted with git worktree remove or rm -rf. Live worktrees are not touched. Returns isError when a teardown failed.",
|
|
220
|
+
inputSchema: {
|
|
221
|
+
dryRun: z
|
|
222
|
+
.boolean()
|
|
223
|
+
.optional()
|
|
224
|
+
.describe("Only report what would happen; changes nothing (not even wtc's registry). Default: false."),
|
|
225
|
+
},
|
|
226
|
+
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false },
|
|
227
|
+
}, async ({ dryRun }, extra) => runTool(extra, (reporter, io) => exclusive(io, async () => {
|
|
228
|
+
const result = await prune({ dryRun }, reporter, io);
|
|
229
|
+
return { isError: result.failures > 0 };
|
|
230
|
+
})));
|
|
231
|
+
server.registerTool("wtc_doctor", {
|
|
232
|
+
title: "Diagnose setup",
|
|
233
|
+
description: "Check git, compose file, config, compose engine and daemon, port isolation, isolation-breaking settings (container_name, shared volumes/networks), port collisions, missing worktrees, orphaned stacks, and busy ports. Returns JSON.",
|
|
234
|
+
inputSchema: {},
|
|
235
|
+
annotations: { readOnlyHint: true, openWorldHint: false },
|
|
236
|
+
}, async (_args, extra) => runTool(extra, async () => {
|
|
237
|
+
const result = await doctor({ live: false });
|
|
238
|
+
return { extra: [JSON.stringify(result, null, 2)] };
|
|
109
239
|
}));
|
|
110
|
-
server
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return {
|
|
115
|
-
content: [{ type: "text", text: "Cleanup complete." }],
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
catch (err) {
|
|
119
|
-
return {
|
|
120
|
-
content: [
|
|
121
|
-
{
|
|
122
|
-
type: "text",
|
|
123
|
-
text: `Cleanup failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
124
|
-
},
|
|
125
|
-
],
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
});
|
|
240
|
+
return server;
|
|
241
|
+
}
|
|
242
|
+
export async function startMcpServer() {
|
|
243
|
+
const server = createMcpServer();
|
|
129
244
|
const transport = new StdioServerTransport();
|
|
130
245
|
await server.connect(transport);
|
|
131
246
|
}
|
package/dist/ports/allocate.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import type { PortMapping, PortAllocation } from "./types.js";
|
|
2
2
|
export declare function allocatePort(defaultPort: number, worktreeIndex: number, portStride?: number): number;
|
|
3
3
|
export declare function allocateWorktreePorts(mappings: PortMapping[], worktreeIndex: number, portStride?: number): PortAllocation[];
|
|
4
|
+
/**
|
|
5
|
+
* Environment variables for a `docker compose` invocation so `${VAR}`
|
|
6
|
+
* interpolation resolves to the allocated ports. Shell environment values take
|
|
7
|
+
* precedence over `.env` files, which makes this independent of the configured
|
|
8
|
+
* env filename — Compose never reads `.env.development` (or any custom name)
|
|
9
|
+
* for interpolation, only the project `.env`, `--env-file`, and the process
|
|
10
|
+
* environment.
|
|
11
|
+
*/
|
|
12
|
+
export declare function allocationEnv(allocations: PortAllocation[]): Record<string, string>;
|
|
4
13
|
export interface PortCollisionParty {
|
|
5
14
|
worktreeIndex: number;
|
|
6
15
|
serviceName: string;
|
|
@@ -11,10 +20,12 @@ export interface PortCollision {
|
|
|
11
20
|
b: PortCollisionParty;
|
|
12
21
|
}
|
|
13
22
|
/**
|
|
14
|
-
* Detect cross-worktree port collisions for a given stride
|
|
15
|
-
* indices 1..
|
|
23
|
+
* Detect cross-worktree port collisions for a given stride. `worktrees` is
|
|
24
|
+
* either a count (indices 1..n) or the exact indices in use — persistent
|
|
25
|
+
* indices can have gaps (e.g. 2, 3, 7), and only the ones actually assigned
|
|
26
|
+
* can collide. Returns one entry per colliding allocation.
|
|
16
27
|
*/
|
|
17
|
-
export declare function detectCrossWorktreeCollisions(mappings: PortMapping[], portStride: number,
|
|
28
|
+
export declare function detectCrossWorktreeCollisions(mappings: PortMapping[], portStride: number, worktrees: number | number[]): PortCollision[];
|
|
18
29
|
/**
|
|
19
30
|
* Smallest portStride that is collision-free across a generous worktree
|
|
20
31
|
* horizon (so the suggestion survives adding more worktrees). Returns -1 if
|