worktree-compose 1.1.1 → 2.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/LICENSE +2 -2
- package/README.md +66 -720
- package/SKILL.md +73 -76
- package/dist/cli.js +85 -10
- package/dist/commands/down.d.ts +2 -0
- package/dist/commands/down.js +7 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +43 -6
- package/dist/commands/passthrough.d.ts +13 -0
- package/dist/commands/passthrough.js +27 -0
- package/dist/commands/stop.d.ts +3 -1
- package/dist/commands/stop.js +2 -2
- package/dist/compose/detect.d.ts +19 -0
- package/dist/compose/detect.js +56 -0
- package/dist/compose/isolation.d.ts +20 -7
- package/dist/compose/isolation.js +88 -36
- package/dist/compose/override.d.ts +54 -4
- package/dist/compose/override.js +179 -52
- package/dist/compose/parse.d.ts +11 -0
- package/dist/compose/parse.js +260 -13
- package/dist/compose/status.d.ts +14 -0
- package/dist/compose/status.js +58 -1
- package/dist/compose/types.d.ts +56 -0
- package/dist/config.d.ts +33 -0
- package/dist/context.d.ts +26 -0
- package/dist/context.js +140 -29
- package/dist/core/clean.d.ts +3 -1
- package/dist/core/clean.js +8 -3
- package/dist/core/compose.d.ts +87 -13
- package/dist/core/compose.js +282 -57
- package/dist/core/doctor.d.ts +2 -8
- package/dist/core/doctor.js +116 -79
- package/dist/core/down.d.ts +37 -0
- package/dist/core/down.js +54 -0
- package/dist/core/health.d.ts +90 -0
- package/dist/core/health.js +198 -0
- package/dist/core/list.d.ts +53 -6
- package/dist/core/list.js +144 -58
- package/dist/core/passthrough.d.ts +69 -0
- package/dist/core/passthrough.js +216 -0
- package/dist/core/promote.d.ts +4 -2
- package/dist/core/promote.js +5 -3
- package/dist/core/restart.d.ts +3 -3
- package/dist/core/restart.js +6 -5
- package/dist/core/seed.d.ts +50 -0
- package/dist/core/seed.js +245 -0
- package/dist/core/shared.d.ts +63 -0
- package/dist/core/shared.js +194 -0
- package/dist/core/start.d.ts +32 -5
- package/dist/core/start.js +297 -49
- package/dist/core/stop.d.ts +3 -1
- package/dist/core/stop.js +6 -3
- package/dist/core/teardown.d.ts +5 -2
- package/dist/core/teardown.js +33 -8
- package/dist/core/types.d.ts +2 -0
- package/dist/git/promote.d.ts +11 -3
- package/dist/git/promote.js +19 -5
- package/dist/mcp/server.js +154 -28
- package/dist/ports/allocate.d.ts +35 -23
- package/dist/ports/allocate.js +108 -58
- package/dist/ports/probe.d.ts +11 -0
- package/dist/ports/probe.js +40 -0
- package/dist/ports/types.d.ts +5 -0
- package/dist/selectors.d.ts +17 -0
- package/dist/selectors.js +19 -0
- package/dist/state/registry.d.ts +23 -3
- package/dist/state/registry.js +57 -17
- package/dist/sync/env.d.ts +42 -2
- package/dist/sync/env.js +79 -8
- package/dist/sync/envsync.d.ts +91 -0
- package/dist/sync/envsync.js +338 -0
- package/dist/sync/files.d.ts +87 -5
- package/dist/sync/files.js +343 -18
- package/dist/utils/paths.d.ts +2 -0
- package/dist/utils/paths.js +9 -0
- package/package.json +9 -2
- package/wtc.schema.json +155 -13
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { listContainers } from "../compose/status.js";
|
|
2
|
+
import { runAsync, tailLines } from "../utils/exec.js";
|
|
3
|
+
import { composeArgs, composeBin } from "../compose/engine.js";
|
|
4
|
+
export function serviceVerdict(s) {
|
|
5
|
+
if (s.state === "running") {
|
|
6
|
+
if (s.health === "unhealthy")
|
|
7
|
+
return "failed";
|
|
8
|
+
return s.health === "starting" ? "pending" : "ready";
|
|
9
|
+
}
|
|
10
|
+
// A one-shot service (a migration, a seed job) that finished cleanly is done.
|
|
11
|
+
// An exit code the engine did not report proves nothing: not ready, not failed.
|
|
12
|
+
if (s.state === "exited")
|
|
13
|
+
return s.exitCode === 0 ? "ready" : s.exitCode === null ? "pending" : "failed";
|
|
14
|
+
if (s.state === "dead")
|
|
15
|
+
return "failed";
|
|
16
|
+
// created (waiting for a dependency), restarting, paused, removing…
|
|
17
|
+
return "pending";
|
|
18
|
+
}
|
|
19
|
+
/** How settled a container state is: among equals, a service is described by its least settled container. */
|
|
20
|
+
const STATE_ORDER = ["dead", "exited", "restarting", "created", "paused", "running"];
|
|
21
|
+
/** How far from ready a verdict is. */
|
|
22
|
+
const VERDICT_ORDER = ["failed", "pending", "ready"];
|
|
23
|
+
/**
|
|
24
|
+
* The services `names` and everything they depend on, as `compose up
|
|
25
|
+
* <names>` brings them up — or null for "every service" (no names).
|
|
26
|
+
* `left` are services the stack does not run (the shared ones): they are
|
|
27
|
+
* not part of it, and neither is what only they depend on.
|
|
28
|
+
*/
|
|
29
|
+
export function serviceClosure(services, names, left = new Set()) {
|
|
30
|
+
if (!names || names.length === 0)
|
|
31
|
+
return null;
|
|
32
|
+
const byName = new Map(services.map((s) => [s.name, s]));
|
|
33
|
+
const out = new Set();
|
|
34
|
+
const visit = (name) => {
|
|
35
|
+
if (out.has(name) || left.has(name))
|
|
36
|
+
return;
|
|
37
|
+
out.add(name);
|
|
38
|
+
for (const dep of Object.keys(byName.get(name)?.dependsOn ?? {}))
|
|
39
|
+
visit(dep);
|
|
40
|
+
};
|
|
41
|
+
names.forEach(visit);
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The services a stack is expected to have a container for: the started
|
|
46
|
+
* selection (with what it depends on), or every service compose starts by
|
|
47
|
+
* itself — not a shared one, not one behind a profile, not one that never
|
|
48
|
+
* has a container.
|
|
49
|
+
*/
|
|
50
|
+
export function expectedServices(services, selection, shared = new Set()) {
|
|
51
|
+
const started = serviceClosure(services, selection, shared);
|
|
52
|
+
return services
|
|
53
|
+
.filter((s) => !shared.has(s.name) && !s.noContainer)
|
|
54
|
+
.filter((s) => (started ? started.has(s.name) : (s.profiles ?? []).length === 0))
|
|
55
|
+
.map((s) => s.name);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The services of `project`, from a container listing (one-off `compose
|
|
59
|
+
* run` containers left out). With `only`, just those services: a stack
|
|
60
|
+
* started with a selection still has the stopped containers of the
|
|
61
|
+
* services left out, and their exit codes say nothing about this start.
|
|
62
|
+
*/
|
|
63
|
+
export function stackServices(containers, project, only = null) {
|
|
64
|
+
const byService = new Map();
|
|
65
|
+
for (const c of containers) {
|
|
66
|
+
if (c.project !== project || c.oneoff)
|
|
67
|
+
continue;
|
|
68
|
+
const name = c.service ?? c.name;
|
|
69
|
+
if (only && !only.has(name))
|
|
70
|
+
continue;
|
|
71
|
+
const own = { service: name, state: c.state, health: c.health, exitCode: c.exitCode, ports: [...c.hostPorts] };
|
|
72
|
+
const existing = byService.get(name);
|
|
73
|
+
if (!existing) {
|
|
74
|
+
byService.set(name, own);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
// Several containers (replicas): the one furthest from ready speaks for the
|
|
78
|
+
// service — a failed one before one still starting before a ready one —
|
|
79
|
+
// and among equals the least settled.
|
|
80
|
+
const distance = (s) => VERDICT_ORDER.indexOf(serviceVerdict(s));
|
|
81
|
+
const rank = (state) => (STATE_ORDER.includes(state) ? STATE_ORDER.indexOf(state) : 0);
|
|
82
|
+
const worse = distance(own) !== distance(existing) ? distance(own) < distance(existing) : rank(own.state) < rank(existing.state);
|
|
83
|
+
byService.set(name, {
|
|
84
|
+
...(worse ? own : existing),
|
|
85
|
+
ports: [...new Set([...existing.ports, ...c.hostPorts])],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return [...byService.values()].sort((a, b) => a.service.localeCompare(b.service));
|
|
89
|
+
}
|
|
90
|
+
/** `web: unhealthy`, `seed: exited (1)`, `api: starting`. */
|
|
91
|
+
export function describeService(s) {
|
|
92
|
+
if (s.state === "running")
|
|
93
|
+
return `${s.service}: ${s.health ?? "running"}`;
|
|
94
|
+
if (s.state === "exited" && s.exitCode !== null)
|
|
95
|
+
return `${s.service}: exited (${s.exitCode})`;
|
|
96
|
+
return `${s.service}: ${s.state}`;
|
|
97
|
+
}
|
|
98
|
+
export function readiness(services) {
|
|
99
|
+
const failed = services.filter((s) => serviceVerdict(s) === "failed");
|
|
100
|
+
const pending = services.filter((s) => serviceVerdict(s) === "pending");
|
|
101
|
+
// A stack with no container at all has nothing left to wait for.
|
|
102
|
+
return { ready: failed.length === 0 && pending.length === 0, services, failed, pending };
|
|
103
|
+
}
|
|
104
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
105
|
+
/**
|
|
106
|
+
* Wait until every service of the stack is ready: running (and healthy,
|
|
107
|
+
* where it has a healthcheck), or finished with exit code 0. Returns as
|
|
108
|
+
* soon as one has failed — unhealthy, or exited with an error — or when
|
|
109
|
+
* the time is up; the result says which services are not ready.
|
|
110
|
+
*/
|
|
111
|
+
export async function waitForStack(engine, project, opts) {
|
|
112
|
+
const list = opts.list ?? (() => listContainers(engine));
|
|
113
|
+
const deadline = Date.now() + opts.timeoutMs;
|
|
114
|
+
let last = "";
|
|
115
|
+
for (;;) {
|
|
116
|
+
if (opts.signal?.aborted)
|
|
117
|
+
throw new Error("Cancelled.");
|
|
118
|
+
let containers;
|
|
119
|
+
try {
|
|
120
|
+
containers = list();
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
// The engine did not answer this once (a slow `ps`): ask again, unless the time is up.
|
|
124
|
+
if (Date.now() >= deadline)
|
|
125
|
+
throw err;
|
|
126
|
+
await sleep(Math.min(opts.pollMs ?? 1000, Math.max(0, deadline - Date.now())));
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const services = stackServices(containers, project, opts.only);
|
|
130
|
+
// An expected service without a container is still to come — where the engine
|
|
131
|
+
// says which service a container belongs to (else none could ever match).
|
|
132
|
+
const own = containers.filter((c) => c.project === project && !c.oneoff);
|
|
133
|
+
if (own.length === 0 || own.some((c) => !!c.service)) {
|
|
134
|
+
for (const name of opts.expected ?? []) {
|
|
135
|
+
if (!services.some((s) => s.service === name)) {
|
|
136
|
+
services.push({ service: name, state: "missing", health: null, exitCode: null, ports: [] });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const state = readiness(services);
|
|
141
|
+
if (state.ready || state.failed.length > 0)
|
|
142
|
+
return { ...state, timedOut: false };
|
|
143
|
+
const waiting = state.pending.map(describeService).join(", ");
|
|
144
|
+
if (waiting !== last) {
|
|
145
|
+
last = waiting;
|
|
146
|
+
opts.onProgress?.(waiting ? `Waiting for ${waiting}` : "Waiting for the stack's containers");
|
|
147
|
+
}
|
|
148
|
+
if (Date.now() >= deadline)
|
|
149
|
+
return { ...state, timedOut: true };
|
|
150
|
+
await sleep(Math.min(opts.pollMs ?? 1000, Math.max(0, deadline - Date.now())));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The last lines each of `services` logged, as an indented block per
|
|
155
|
+
* service — what a person (or an agent) needs to see why a stack did not
|
|
156
|
+
* come up, without a second command. Empty when nothing could be read.
|
|
157
|
+
*/
|
|
158
|
+
export async function serviceLogTails(engine, project, services, call, lines = 15) {
|
|
159
|
+
const blocks = [];
|
|
160
|
+
for (const service of services.slice(0, 5)) {
|
|
161
|
+
try {
|
|
162
|
+
const result = await runAsync(composeBin(engine), composeArgs(engine, project, "logs", "--no-color", "--tail", String(lines), service), { cwd: call.cwd, env: mergedEnv(call.env), signal: call.signal, timeoutMs: 20_000 });
|
|
163
|
+
const text = tailLines(`${result.stdout}\n${result.stderr}`.trim(), lines);
|
|
164
|
+
if (text)
|
|
165
|
+
blocks.push(`--- ${service}: last ${lines} log lines ---\n${text}`);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// No logs is not worth failing a diagnosis for.
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return blocks.join("\n");
|
|
172
|
+
}
|
|
173
|
+
function mergedEnv(env) {
|
|
174
|
+
const out = { ...process.env, ...env };
|
|
175
|
+
for (const key of Object.keys(out))
|
|
176
|
+
if (out[key] === undefined)
|
|
177
|
+
delete out[key];
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Why a stack is not up, in a few lines: each service that failed or is
|
|
182
|
+
* still starting, then the failed ones' last log lines.
|
|
183
|
+
*/
|
|
184
|
+
export async function diagnoseStack(engine, project, call, list = () => listContainers(engine), only = null) {
|
|
185
|
+
let state;
|
|
186
|
+
try {
|
|
187
|
+
state = readiness(stackServices(list(), project, only));
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return "";
|
|
191
|
+
}
|
|
192
|
+
const troubled = [...state.failed, ...state.pending];
|
|
193
|
+
if (troubled.length === 0)
|
|
194
|
+
return "";
|
|
195
|
+
const summary = `Not ready: ${troubled.map(describeService).join(", ")}.`;
|
|
196
|
+
const logs = await serviceLogTails(engine, project, state.failed.map((s) => s.service), call);
|
|
197
|
+
return logs ? `${summary}\n${logs}` : summary;
|
|
198
|
+
}
|
package/dist/core/list.d.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
import type { StackStatus } from "../compose/status.js";
|
|
2
|
+
import type { ComposeService } from "../compose/types.js";
|
|
2
3
|
import type { PortAllocation } from "../ports/types.js";
|
|
4
|
+
import type { WorktreeSelector } from "../selectors.js";
|
|
3
5
|
import type { CoreIo } from "./types.js";
|
|
6
|
+
export interface ListedPort {
|
|
7
|
+
/** Host port of this worktree's stack. */
|
|
8
|
+
host: number;
|
|
9
|
+
/** Port inside the container. */
|
|
10
|
+
container: number;
|
|
11
|
+
/** `http(s)://localhost:<host>`, or null for a port that evidently is not HTTP (a database's, a broker's). */
|
|
12
|
+
url: string | null;
|
|
13
|
+
}
|
|
14
|
+
export interface ListedService {
|
|
15
|
+
name: string;
|
|
16
|
+
/**
|
|
17
|
+
* `running`, `exited`, `created`, `restarting`… as the engine reports
|
|
18
|
+
* it; `down` when the service has no container; `shared` when the stack
|
|
19
|
+
* uses the main checkout's instance.
|
|
20
|
+
*/
|
|
21
|
+
state: string;
|
|
22
|
+
/** `healthy`, `unhealthy`, `starting`, or null without a healthcheck (or when not running). */
|
|
23
|
+
health: "healthy" | "unhealthy" | "starting" | null;
|
|
24
|
+
/** Exit code of a service that has exited. */
|
|
25
|
+
exitCode: number | null;
|
|
26
|
+
/** Host ports allocated to the service in this worktree. */
|
|
27
|
+
ports: ListedPort[];
|
|
28
|
+
}
|
|
4
29
|
export interface ListRow {
|
|
5
30
|
index: number;
|
|
6
31
|
branch: string;
|
|
@@ -10,12 +35,22 @@ export interface ListRow {
|
|
|
10
35
|
status: StackStatus;
|
|
11
36
|
running: number | null;
|
|
12
37
|
total: number | null;
|
|
38
|
+
/**
|
|
39
|
+
* Whether the running stack is usable: `healthy` (every service ready),
|
|
40
|
+
* `starting` (some still coming up), `unhealthy` (one failed its
|
|
41
|
+
* healthcheck or exited with an error); null when nothing runs.
|
|
42
|
+
*/
|
|
43
|
+
health: "healthy" | "unhealthy" | "starting" | null;
|
|
13
44
|
url: string | null;
|
|
14
45
|
/**
|
|
15
46
|
* Allocated host ports by variable — or by `<service>:<default port>` for
|
|
16
47
|
* hardcoded ports remapped at runtime.
|
|
17
48
|
*/
|
|
18
49
|
ports: Record<string, number>;
|
|
50
|
+
/** Every service of the stack's compose file, with its state and ports. */
|
|
51
|
+
services: ListedService[];
|
|
52
|
+
/** The worktree is on a detached HEAD (`branch` is then "detached"). */
|
|
53
|
+
detached: boolean;
|
|
19
54
|
/** The worktree was removed but its registry record (and maybe its stack) remains. */
|
|
20
55
|
orphan: boolean;
|
|
21
56
|
/**
|
|
@@ -35,12 +70,24 @@ export interface ListResult {
|
|
|
35
70
|
warnings: string[];
|
|
36
71
|
isolationWarnings: string[];
|
|
37
72
|
}
|
|
73
|
+
export interface ListOptions {
|
|
74
|
+
/** Only these worktrees (index, branch, dir name, path, "."). */
|
|
75
|
+
worktrees?: WorktreeSelector[];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `https` when the service evidently speaks TLS on that port — its
|
|
79
|
+
* healthcheck asks for `https://…:<port>`, or the port is 443 / 8443 —
|
|
80
|
+
* else `http`.
|
|
81
|
+
*/
|
|
82
|
+
export declare function portScheme(service: ComposeService | undefined, containerPort: number): "http" | "https";
|
|
38
83
|
/** Guess the URL a human wants: a frontend-ish service, else the last port. */
|
|
39
|
-
export declare function pickUrl(allocations: PortAllocation[]): string | null;
|
|
84
|
+
export declare function pickUrl(allocations: PortAllocation[], services?: ComposeService[]): string | null;
|
|
40
85
|
/**
|
|
41
|
-
* Worktrees (and orphaned stacks) with status, ports, and URL.
|
|
42
|
-
* (wtc_list carries readOnlyHint): identities of worktrees seen
|
|
43
|
-
* first time are computed, adopting running stacks, but never
|
|
44
|
-
* no registry write, no `wtc-id`, a corrupt registry left in place.
|
|
86
|
+
* Worktrees (and orphaned stacks) with status, services, ports, and URL.
|
|
87
|
+
* Read-only (wtc_list carries readOnlyHint): identities of worktrees seen
|
|
88
|
+
* for the first time are computed, adopting running stacks, but never
|
|
89
|
+
* recorded — no registry write, no `wtc-id`, a corrupt registry left in place.
|
|
45
90
|
*/
|
|
46
|
-
export declare function listWorktrees(io: CoreIo): ListResult;
|
|
91
|
+
export declare function listWorktrees(io: CoreIo, options?: ListOptions): ListResult;
|
|
92
|
+
/** How a worktree is named in a table: its branch, or its directory when the HEAD is detached. */
|
|
93
|
+
export declare function worktreeLabel(row: Pick<ListRow, "branch" | "detached" | "path">): string;
|
package/dist/core/list.js
CHANGED
|
@@ -1,90 +1,169 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
-
import
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { buildContext, HOST_PORTS_SERVICE } from "../context.js";
|
|
3
4
|
import { allocatableMappings, portsByLabel } from "../ports/allocate.js";
|
|
4
|
-
import { stackAllocations, stackRemap } from "./compose.js";
|
|
5
|
+
import { stackAllocations, stackRemap, stackView } from "./compose.js";
|
|
6
|
+
import { expectedServices, readiness, serviceClosure, stackServices } from "./health.js";
|
|
5
7
|
import { stackStatus } from "../compose/status.js";
|
|
8
|
+
import { selectWorktrees } from "../selectors.js";
|
|
6
9
|
/**
|
|
7
10
|
* A stack's ports as `start` allocates them, run from `dir` (its worktree,
|
|
8
11
|
* or the main checkout when the worktree is gone): read-only.
|
|
9
12
|
*/
|
|
10
13
|
function safeAllocate(ctx, index, dir, project) {
|
|
11
14
|
try {
|
|
12
|
-
return stackAllocations(ctx, index, stackRemap(ctx, dir, project).targets).allocations;
|
|
15
|
+
return stackAllocations(ctx, { index, dir, project }, stackRemap(ctx, dir, project).targets).allocations;
|
|
13
16
|
}
|
|
14
17
|
catch {
|
|
15
18
|
return [];
|
|
16
19
|
}
|
|
17
20
|
}
|
|
21
|
+
/** The stack's services as its compose file declares them (the main checkout's when the directory's is unreadable). */
|
|
22
|
+
function declaredServices(ctx, dir) {
|
|
23
|
+
try {
|
|
24
|
+
return stackView(ctx, dir).composeFile.services;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return ctx.composeFile.services;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* `https` when the service evidently speaks TLS on that port — its
|
|
32
|
+
* healthcheck asks for `https://…:<port>`, or the port is 443 / 8443 —
|
|
33
|
+
* else `http`.
|
|
34
|
+
*/
|
|
35
|
+
export function portScheme(service, containerPort) {
|
|
36
|
+
if (containerPort === 443 || containerPort === 8443)
|
|
37
|
+
return "https";
|
|
38
|
+
const check = service?.healthcheck ?? "";
|
|
39
|
+
if (new RegExp(`https://[^\\s/"']*:${containerPort}(?!\\d)`).test(check))
|
|
40
|
+
return "https";
|
|
41
|
+
return "http";
|
|
42
|
+
}
|
|
43
|
+
/** Container ports of services that do not speak HTTP: Postgres, MySQL, Redis, MongoDB, Kafka, AMQP, memcached, SMTP, SSH… */
|
|
44
|
+
const NOT_HTTP = new Set([22, 25, 465, 587, 1025, 1433, 1521, 3306, 5432, 5672, 6379, 9042, 9092, 9094, 11211, 26257, 27017]);
|
|
45
|
+
/** The URL a browser or an HTTP client would use for the port, or null when it is not an HTTP port. */
|
|
46
|
+
function portUrl(service, a) {
|
|
47
|
+
if (NOT_HTTP.has(a.containerPort))
|
|
48
|
+
return null;
|
|
49
|
+
return `${portScheme(service, a.containerPort)}://localhost:${a.port}`;
|
|
50
|
+
}
|
|
18
51
|
/** Guess the URL a human wants: a frontend-ish service, else the last port. */
|
|
19
|
-
export function pickUrl(allocations) {
|
|
52
|
+
export function pickUrl(allocations, services = []) {
|
|
20
53
|
const frontend = allocations.find((a) => a.serviceName.includes("frontend") ||
|
|
21
54
|
a.serviceName.includes("web") ||
|
|
22
55
|
a.serviceName.includes("app") ||
|
|
23
56
|
a.serviceName.includes("ui"));
|
|
24
|
-
|
|
25
|
-
|
|
57
|
+
// Else the app the developer runs on the host (the config's `ports`), else the
|
|
58
|
+
// last port that could be a web page (never a database's).
|
|
59
|
+
const chosen = frontend ??
|
|
60
|
+
allocations.find((a) => a.serviceName === HOST_PORTS_SERVICE) ??
|
|
61
|
+
[...allocations].reverse().find((a) => !NOT_HTTP.has(a.containerPort));
|
|
62
|
+
return chosen ? portUrl(services.find((s) => s.name === chosen.serviceName), chosen) : null;
|
|
63
|
+
}
|
|
64
|
+
function listedServices(declared, shared, allocations, states) {
|
|
65
|
+
const byName = new Map(states.map((s) => [s.service, s]));
|
|
66
|
+
const names = [...new Set([...declared.map((s) => s.name), ...states.map((s) => s.service)])];
|
|
67
|
+
const out = names.map((name) => {
|
|
68
|
+
const svc = declared.find((s) => s.name === name);
|
|
69
|
+
const state = byName.get(name);
|
|
70
|
+
return {
|
|
71
|
+
name,
|
|
72
|
+
state: shared.has(name) ? "shared" : (state?.state ?? "down"),
|
|
73
|
+
health: state?.health ?? null,
|
|
74
|
+
exitCode: state?.exitCode ?? null,
|
|
75
|
+
ports: allocations
|
|
76
|
+
.filter((a) => a.serviceName === name)
|
|
77
|
+
.map((a) => ({ host: a.port, container: a.containerPort, url: portUrl(svc, a) })),
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
// Ports for processes outside compose (the config's `ports`) are the host's, not a service's.
|
|
81
|
+
const host = allocations.filter((a) => a.serviceName === HOST_PORTS_SERVICE);
|
|
82
|
+
if (host.length > 0) {
|
|
83
|
+
out.push({
|
|
84
|
+
name: HOST_PORTS_SERVICE,
|
|
85
|
+
state: "host",
|
|
86
|
+
health: null,
|
|
87
|
+
exitCode: null,
|
|
88
|
+
ports: host.map((a) => ({ host: a.port, container: a.containerPort, url: `http://localhost:${a.port}` })),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
26
92
|
}
|
|
27
93
|
/**
|
|
28
|
-
* Worktrees (and orphaned stacks) with status, ports, and URL.
|
|
29
|
-
* (wtc_list carries readOnlyHint): identities of worktrees seen
|
|
30
|
-
* first time are computed, adopting running stacks, but never
|
|
31
|
-
* no registry write, no `wtc-id`, a corrupt registry left in place.
|
|
94
|
+
* Worktrees (and orphaned stacks) with status, services, ports, and URL.
|
|
95
|
+
* Read-only (wtc_list carries readOnlyHint): identities of worktrees seen
|
|
96
|
+
* for the first time are computed, adopting running stacks, but never
|
|
97
|
+
* recorded — no registry write, no `wtc-id`, a corrupt registry left in place.
|
|
32
98
|
*/
|
|
33
|
-
export function listWorktrees(io) {
|
|
99
|
+
export function listWorktrees(io, options = {}) {
|
|
34
100
|
const ctx = buildContext({}, { cwd: io.cwd, probeStacks: true, readOnly: true });
|
|
35
101
|
const warnings = [...ctx.warnings];
|
|
36
102
|
const stacks = ctx.stacks;
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
103
|
+
const containers = stacks.containers() ?? [];
|
|
104
|
+
const only = options.worktrees && options.worktrees.length > 0
|
|
105
|
+
? new Set(selectWorktrees(ctx.worktrees, options.worktrees, ctx.cwd).map((w) => w.project))
|
|
106
|
+
: null;
|
|
107
|
+
const row = (entry, dir, extra) => {
|
|
108
|
+
const allocations = safeAllocate(ctx, entry.index, dir, entry.project);
|
|
109
|
+
const state = stacks.state(entry.project);
|
|
110
|
+
const declared = declaredServices(ctx, dir);
|
|
111
|
+
const states = stackServices(containers, entry.project);
|
|
112
|
+
// A stack started with a selection is judged by those services: the others are stopped on purpose.
|
|
113
|
+
const started = serviceClosure(declared, entry.services, ctx.sharedServices);
|
|
114
|
+
const ready = readiness(started ? states.filter((s) => started.has(s.service)) : states);
|
|
115
|
+
const anyRunning = states.some((s) => s.state === "running");
|
|
116
|
+
// A service the stack should run but that has no container (removed, never
|
|
117
|
+
// created) is a missing part of it, whatever the containers that exist say.
|
|
118
|
+
// Only where that can be told: a worktree that is there (its compose file is
|
|
119
|
+
// the one read), on an engine that says which service a container belongs to.
|
|
120
|
+
const labelled = containers.some((c) => c.project === entry.project && !c.oneoff && !!c.service);
|
|
121
|
+
const missing = extra.live && labelled
|
|
122
|
+
? expectedServices(declared, entry.services, ctx.sharedServices).filter((name) => !states.some((s) => s.service === name))
|
|
123
|
+
: [];
|
|
124
|
+
// A one-shot service that finished cleanly (a migration) is not a missing part
|
|
125
|
+
// of the stack: with everything else running, the stack is up.
|
|
126
|
+
const settled = anyRunning &&
|
|
127
|
+
missing.length === 0 &&
|
|
128
|
+
states.every((s) => s.state === "running" || (s.state === "exited" && s.exitCode === 0));
|
|
129
|
+
const status = settled ? "up" : stackStatus(state);
|
|
130
|
+
return {
|
|
131
|
+
index: entry.index,
|
|
132
|
+
branch: entry.branch,
|
|
133
|
+
path: entry.path,
|
|
134
|
+
project: entry.project,
|
|
135
|
+
status: anyRunning && missing.length > 0 && status === "up" ? "partial" : status,
|
|
47
136
|
running: state?.running ?? null,
|
|
48
137
|
total: state?.total ?? null,
|
|
49
|
-
|
|
138
|
+
health: !anyRunning
|
|
139
|
+
? null
|
|
140
|
+
: ready.failed.length > 0 || missing.length > 0
|
|
141
|
+
? "unhealthy"
|
|
142
|
+
: ready.pending.length > 0
|
|
143
|
+
? "starting"
|
|
144
|
+
: "healthy",
|
|
145
|
+
url: extra.live ? pickUrl(allocations, declared) : null,
|
|
50
146
|
ports: portsByLabel(allocations),
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
project: u.project,
|
|
63
|
-
status: stackStatus(state),
|
|
64
|
-
running: state?.running ?? null,
|
|
65
|
-
total: state?.total ?? null,
|
|
66
|
-
url: null,
|
|
67
|
-
ports: portsByLabel(allocations),
|
|
68
|
-
orphan: false,
|
|
69
|
-
unavailable: u.reason,
|
|
70
|
-
});
|
|
147
|
+
services: listedServices(declared, ctx.sharedServices, allocations, states),
|
|
148
|
+
detached: extra.detached,
|
|
149
|
+
orphan: extra.orphan,
|
|
150
|
+
unavailable: extra.unavailable,
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
const rows = [];
|
|
154
|
+
for (const wt of ctx.worktrees) {
|
|
155
|
+
if (only && !only.has(wt.project))
|
|
156
|
+
continue;
|
|
157
|
+
rows.push(row(wt, wt.path, { orphan: false, unavailable: null, detached: wt.detached, live: true }));
|
|
71
158
|
}
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
status: stackStatus(state),
|
|
81
|
-
running: state?.running ?? null,
|
|
82
|
-
total: state?.total ?? null,
|
|
83
|
-
url: null,
|
|
84
|
-
ports: portsByLabel(allocations),
|
|
85
|
-
orphan: true,
|
|
86
|
-
unavailable: null,
|
|
87
|
-
});
|
|
159
|
+
if (!only) {
|
|
160
|
+
for (const u of ctx.unavailable) {
|
|
161
|
+
rows.push(row(u, u.path, { orphan: false, unavailable: u.reason, detached: false, live: false }));
|
|
162
|
+
}
|
|
163
|
+
for (const o of ctx.orphans) {
|
|
164
|
+
const dir = fs.existsSync(o.path) ? o.path : ctx.repoRoot;
|
|
165
|
+
rows.push(row(o, dir, { orphan: true, unavailable: null, detached: false, live: false }));
|
|
166
|
+
}
|
|
88
167
|
}
|
|
89
168
|
rows.sort((a, b) => a.index - b.index);
|
|
90
169
|
if (rows.length > 0 && stacks.problem)
|
|
@@ -99,3 +178,10 @@ export function listWorktrees(io) {
|
|
|
99
178
|
isolationWarnings: ctx.isolationWarnings,
|
|
100
179
|
};
|
|
101
180
|
}
|
|
181
|
+
/** How a worktree is named in a table: its branch, or its directory when the HEAD is detached. */
|
|
182
|
+
export function worktreeLabel(row) {
|
|
183
|
+
if (!row.detached)
|
|
184
|
+
return row.branch;
|
|
185
|
+
const parts = path.resolve(row.path).split(path.sep).filter(Boolean);
|
|
186
|
+
return `detached (${parts.slice(-2).join("/")})`;
|
|
187
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { ComposeCallEnv } from "./compose.js";
|
|
2
|
+
import type { ComposeEngine } from "../compose/engine.js";
|
|
3
|
+
import type { WorktreeSelector } from "../selectors.js";
|
|
4
|
+
import type { ResolvedWorktree } from "../state/registry.js";
|
|
5
|
+
import type { CoreIo } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Compose commands on one worktree's stack — `exec`, `logs`, or anything
|
|
8
|
+
* else — with the stack's project name, compose files and ports, from any
|
|
9
|
+
* directory. A plain `docker compose` only reaches the right stack from
|
|
10
|
+
* the worktree's root, and a `docker exec web …` copied from the project's
|
|
11
|
+
* scripts reaches the main checkout's container, because worktree
|
|
12
|
+
* containers are not named `web`. These always reach the worktree's own.
|
|
13
|
+
*/
|
|
14
|
+
export interface StackCall {
|
|
15
|
+
engine: ComposeEngine;
|
|
16
|
+
worktree: ResolvedWorktree;
|
|
17
|
+
env: ComposeCallEnv;
|
|
18
|
+
/** Directories the main checkout's own stack may run from. */
|
|
19
|
+
mainDirs: string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The stack a command addresses: the named worktree's, else the one the
|
|
23
|
+
* command runs in. From the main checkout a worktree must be named.
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveStackCall(worktree: WorktreeSelector | undefined, io: CoreIo, opts?: {
|
|
26
|
+
readOnly?: boolean;
|
|
27
|
+
}): StackCall;
|
|
28
|
+
/** The compose command in `args` (`down`, `ps`…), past compose's own options, and whether it is a dry run. */
|
|
29
|
+
export declare function composeSubcommand(args: string[]): {
|
|
30
|
+
command: string | null;
|
|
31
|
+
dryRun: boolean;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Run `compose -p <project> <args…>` attached to the terminal and return
|
|
35
|
+
* its exit code: the command's own result, passed through as it is.
|
|
36
|
+
*/
|
|
37
|
+
export declare function runAttached(call: StackCall, args: string[]): Promise<number>;
|
|
38
|
+
export interface CapturedRun {
|
|
39
|
+
exitCode: number;
|
|
40
|
+
/** The end of what the command printed, stdout and stderr in the order they came, bounded. */
|
|
41
|
+
output: string;
|
|
42
|
+
/** Output was cut: only its end is in `output`. */
|
|
43
|
+
truncated: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Run `compose -p <project> <args…>` and capture the end of its output
|
|
47
|
+
* (for the MCP server, whose stdout is the protocol). A non-zero exit is a
|
|
48
|
+
* result, not an error; only a command that could not run at all, timed
|
|
49
|
+
* out, or was cancelled throws.
|
|
50
|
+
*/
|
|
51
|
+
export declare function runCaptured(call: StackCall, args: string[], opts?: {
|
|
52
|
+
signal?: AbortSignal;
|
|
53
|
+
timeoutMs?: number;
|
|
54
|
+
maxLines?: number;
|
|
55
|
+
killGraceMs?: number;
|
|
56
|
+
}): Promise<CapturedRun>;
|
|
57
|
+
/** A service name is a name: one that starts with `-` would be read by compose as an option. */
|
|
58
|
+
export declare function assertServiceName(service: string): void;
|
|
59
|
+
/** `exec` arguments: no TTY allocation unless the terminal is interactive. */
|
|
60
|
+
export declare function execArgs(service: string, command: string[], interactive: boolean): string[];
|
|
61
|
+
export interface LogsOptions {
|
|
62
|
+
services?: string[];
|
|
63
|
+
/** Lines from the end of each service's log (default 100). */
|
|
64
|
+
tail?: number;
|
|
65
|
+
/** Only logs newer than this (`10m`, `2h`, or a timestamp), as compose reads it. */
|
|
66
|
+
since?: string;
|
|
67
|
+
follow?: boolean;
|
|
68
|
+
}
|
|
69
|
+
export declare function logsArgs(options: LogsOptions): string[];
|