werkmeister-core 0.94.2 → 0.94.3

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,12 @@
1
1
  # sandbox-core
2
2
 
3
+ ## 0.94.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 31dd7fd: Shared infra made from an old checkout path is detected. A container made from another checkout's compose file is named, with `werkmeister infra up` as the fix, by `feature up`, `infra proxy-reload`, `feature status` and `infra status`. `infra up` now recreates those containers too, including one whose only drift is the files it was made from. Caddy counts as unhealthy when its `/__werkmeister` picker page is missing from its mount, which used to 404 on every routed host.
8
+ - 879f709: `feature stop --via-daemon` (the SessionEnd auto-stop) calls the daemon even when the CLI can't find its own daemon build for the build header. The lookup failure used to throw before the request, and the error was dropped, so the stop did nothing and exited 0. It now sends no header, and any daemon error other than "not running" is printed.
9
+
3
10
  ## 0.94.2
4
11
 
5
12
  ### Patch Changes
package/dist/caddy.js CHANGED
@@ -7,7 +7,7 @@
7
7
  import * as fs from "node:fs";
8
8
  import { Colors } from "./colors.js";
9
9
  import { stopOrRestart } from "./compose.js";
10
- import { composeInvocation, INFRA_PROJECT } from "./compose_infra.js";
10
+ import { composeInvocation, INFRA_PROJECT, warnStaleInfra } from "./compose_infra.js";
11
11
  import { sharedProxySkipReason, stateInfraRoot } from "./config.js";
12
12
  import { copyFileToContainer, execCapture, projectContainers } from "./docker.js";
13
13
  import { ProcError, run } from "./proc.js";
@@ -297,6 +297,8 @@ export async function reloadCaddy() {
297
297
  info(`Caddy reload skipped (${skip})`);
298
298
  return true;
299
299
  }
300
+ // A reload only copies the Caddyfile: it can't fix a container still mounting a moved checkout (SW-610).
301
+ await warnStaleInfra();
300
302
  const hostCaddyfile = `${stateInfraRoot()}/proxy/Caddyfile`;
301
303
  info("Copying Caddyfile into Caddy container...");
302
304
  if (!(await cpCaddyfileToContainer(hostCaddyfile))) {
@@ -11,6 +11,7 @@
11
11
  * all call this, and `infra.ts` already imports `k8s.ts`, so putting it in
12
12
  * `infra.ts` would make a cycle.
13
13
  */
14
+ import type Dockerode from "dockerode";
14
15
  /** The Compose project name for shared infra (shared-infra.yml's `name:`). */
15
16
  export declare const INFRA_PROJECT = "w9r-infra";
16
17
  /**
@@ -31,3 +32,34 @@ export declare function composeInvocation(): {
31
32
  env: NodeJS.ProcessEnv;
32
33
  cwd: string;
33
34
  };
35
+ /** An infra container left over from another checkout (SW-610). */
36
+ export interface StaleInfraContainer {
37
+ name: string;
38
+ service: string;
39
+ reason: string;
40
+ }
41
+ /**
42
+ * Why *c* no longer matches the shipped compose file at *composeFile*, or null.
43
+ * Compose stamps the files it ran with; after a checkout moves, a container made
44
+ * from the old path keeps its relative bind mounts (the Caddy picker UI) pointed
45
+ * there, and `feature up` / `proxy-reload` never recreate it. The label, not the
46
+ * bind sources: a missing source comes back as an empty dir on restart, and from
47
+ * a container the host's paths don't exist at all.
48
+ */
49
+ export declare function staleInfraReason(c: Pick<Dockerode.ContainerInfo, "Labels">, composeFile: string): string | null;
50
+ /**
51
+ * The shared-infra containers {@link staleInfraReason} flags. Never throws: empty when
52
+ * docker is unreachable or there is no machine config to say where the compose file is.
53
+ */
54
+ export declare function staleInfraContainers(): Promise<StaleInfraContainer[]>;
55
+ /** One warning per stale infra container, naming the command that recreates it. */
56
+ export declare function warnStaleInfra(): Promise<void>;
57
+ /**
58
+ * Split *stale* into the services `up` can recreate (those in *started*, this run's list)
59
+ * and leftovers whose service this checkout's file no longer has: naming one to
60
+ * `compose up` fails every run, or turns on a profile that is off.
61
+ */
62
+ export declare function recreatePlan(stale: StaleInfraContainer[], started: string[]): {
63
+ recreate: string[];
64
+ leftover: StaleInfraContainer[];
65
+ };
@@ -14,8 +14,11 @@
14
14
  import * as fs from "node:fs";
15
15
  import * as path from "node:path";
16
16
  import { baselineOverridePath } from "./baseline.js";
17
- import { infraRoot, loadConfig, stateInfraRoot } from "./config.js";
17
+ import { infraRoot, isFixture, loadConfig, stateInfraRoot } from "./config.js";
18
+ import { docker } from "./docker.js";
19
+ import { insideContainer } from "./global_config.js";
18
20
  import { generateK8sForwards, overrideFilePath } from "./k8s_forwards.js";
21
+ import { warn } from "./reporter.js";
19
22
  /** The Compose project name for shared infra (shared-infra.yml's `name:`). */
20
23
  export const INFRA_PROJECT = "w9r-infra";
21
24
  /**
@@ -73,3 +76,62 @@ export function composeInvocation() {
73
76
  cwd: path.dirname(stateInfraRoot()),
74
77
  };
75
78
  }
79
+ /**
80
+ * Why *c* no longer matches the shipped compose file at *composeFile*, or null.
81
+ * Compose stamps the files it ran with; after a checkout moves, a container made
82
+ * from the old path keeps its relative bind mounts (the Caddy picker UI) pointed
83
+ * there, and `feature up` / `proxy-reload` never recreate it. The label, not the
84
+ * bind sources: a missing source comes back as an empty dir on restart, and from
85
+ * a container the host's paths don't exist at all.
86
+ */
87
+ export function staleInfraReason(c, composeFile) {
88
+ const from = c.Labels?.["com.docker.compose.project.config_files"]?.split(",")[0];
89
+ return from && path.resolve(from) !== path.resolve(composeFile) ? `created from ${from}` : null;
90
+ }
91
+ /**
92
+ * The shared-infra containers {@link staleInfraReason} flags. Never throws: empty when
93
+ * docker is unreachable or there is no machine config to say where the compose file is.
94
+ */
95
+ export async function staleInfraContainers() {
96
+ // A workspace container's checkout is /app, so every host-made label differs, and a
97
+ // recreate from there would mount /app paths the host lacks (SW-554). Fixtures never start infra.
98
+ if (insideContainer() && !isFixture())
99
+ return [];
100
+ let list;
101
+ let composeFile;
102
+ try {
103
+ list = await docker().listContainers({
104
+ all: true,
105
+ filters: { label: [`com.docker.compose.project=${INFRA_PROJECT}`] },
106
+ });
107
+ composeFile = composeInvocation().argv[3];
108
+ }
109
+ catch {
110
+ return [];
111
+ }
112
+ return list.flatMap((c) => {
113
+ const reason = staleInfraReason(c, composeFile);
114
+ const name = c.Names?.[0]?.replace(/^\//, "") ?? c.Id.slice(0, 12);
115
+ return reason
116
+ ? [{ name, service: c.Labels?.["com.docker.compose.service"] ?? "", reason }]
117
+ : [];
118
+ });
119
+ }
120
+ /** One warning per stale infra container, naming the command that recreates it. */
121
+ export async function warnStaleInfra() {
122
+ for (const s of await staleInfraContainers()) {
123
+ warn(`${s.name} was ${s.reason}, not this checkout: run \`werkmeister infra up\` to recreate it`);
124
+ }
125
+ }
126
+ /**
127
+ * Split *stale* into the services `up` can recreate (those in *started*, this run's list)
128
+ * and leftovers whose service this checkout's file no longer has: naming one to
129
+ * `compose up` fails every run, or turns on a profile that is off.
130
+ */
131
+ export function recreatePlan(stale, started) {
132
+ const known = stale.filter((s) => started.includes(s.service));
133
+ return {
134
+ recreate: [...new Set(known.map((s) => s.service))],
135
+ leftover: stale.filter((s) => !started.includes(s.service)),
136
+ };
137
+ }
@@ -173,6 +173,15 @@ let staleWarned = false;
173
173
  export function expectDaemonBuild(get) {
174
174
  expectedBuild = get;
175
175
  }
176
+ // The header is a hint: a lookup that throws (no checkout to find, SW-718) sends none rather than dropping the call.
177
+ function buildToSend() {
178
+ try {
179
+ return expectedBuild?.() ?? null;
180
+ }
181
+ catch {
182
+ return null;
183
+ }
184
+ }
176
185
  function checkBuild(onDisk, res) {
177
186
  const loaded = res.headers.get(BUILD_HEADER);
178
187
  if (staleWarned || !onDisk || !loaded || loaded === onDisk)
@@ -241,7 +250,7 @@ export async function daemonStatus() {
241
250
  */
242
251
  export async function callTool(name, args = {}, opts = {}) {
243
252
  const timeout = AbortSignal.timeout(opts.timeoutMs ?? 120_000);
244
- const build = expectedBuild?.() ?? null;
253
+ const build = buildToSend();
245
254
  const res = await send(`${daemonUrl()}/api/tool`, {
246
255
  method: "POST",
247
256
  headers: {
package/dist/infra.js CHANGED
@@ -21,10 +21,11 @@ import * as net from "node:net";
21
21
  import * as os from "node:os";
22
22
  import * as path from "node:path";
23
23
  import { baselineSpecs, generateBaselineOverride } from "./baseline.js";
24
+ import { CADDY_CONTAINER } from "./caddy.js";
24
25
  import { stopOrRestart, takeDownProject } from "./compose.js";
25
- import { composeInvocation, INFRA_PROJECT, infraProfiles, LEGACY_INFRA_PROJECT, } from "./compose_infra.js";
26
+ import { composeInvocation, INFRA_PROJECT, infraProfiles, LEGACY_INFRA_PROJECT, recreatePlan, staleInfraContainers, } from "./compose_infra.js";
26
27
  import { isFixture, sandboxHome } from "./config.js";
27
- import { projectContainers } from "./docker.js";
28
+ import { execCapture, projectContainers } from "./docker.js";
28
29
  import { generateCaddyfile } from "./generate.js";
29
30
  import { configureGitHooksPath } from "./git_hooks.js";
30
31
  import { checkPortForwardHealth, ensurePortForwardsHealthy, refreshKubeconfig } from "./k8s.js";
@@ -145,13 +146,13 @@ export async function startSharedInfra(signal) {
145
146
  return false;
146
147
  }
147
148
  takeDownProject(LEGACY_INFRA_PROJECT, "the pre-rename shared infra");
148
- const services = [
149
+ const started = [
149
150
  "k8s-forwards",
150
151
  "caddy",
151
152
  ...infraProfiles(),
152
153
  ...baselineSpecs().map((s) => `baseline-${s.service}`),
153
- ].join(", ");
154
- info(`Starting shared infrastructure (${services})...`);
154
+ ];
155
+ info(`Starting shared infrastructure (${started.join(", ")})...`);
155
156
  try {
156
157
  const result = await runAsync([...argv, "up", "-d", "--build"], {
157
158
  capture: true,
@@ -163,6 +164,20 @@ export async function startSharedInfra(signal) {
163
164
  error(`docker compose failed:\n${result.stderr.trim()}`);
164
165
  return false;
165
166
  }
167
+ // `up` recreates a container whose resolved binds changed, not one whose only drift is
168
+ // the compose files it was made from (w9r-k8s-forwards after a checkout move, SW-610).
169
+ const { recreate, leftover } = recreatePlan(await staleInfraContainers(), started);
170
+ for (const s of leftover) {
171
+ warn(`${s.name} was ${s.reason}, and this checkout has no such service: remove it with \`docker rm -f ${s.name}\``);
172
+ }
173
+ if (recreate.length) {
174
+ info(`Recreating ${recreate.join(", ")} from this checkout...`);
175
+ const again = await runAsync([...argv, "up", "-d", "--no-deps", "--force-recreate", ...recreate], { capture: true, signal, env, cwd });
176
+ if (again.status !== 0) {
177
+ error(`docker compose failed:\n${again.stderr.trim()}`);
178
+ return false;
179
+ }
180
+ }
166
181
  success("Shared infrastructure started");
167
182
  return true;
168
183
  }
@@ -178,6 +193,8 @@ export async function startSharedInfra(signal) {
178
193
  throw e;
179
194
  }
180
195
  }
196
+ /** The page Caddy serves for `/__werkmeister` (generate.ts's `root * /werkmeister/proxy/ui`). */
197
+ const PICKER_PAGE = "/werkmeister/proxy/ui/index.html";
181
198
  function checkPort(host, port, timeoutSec) {
182
199
  return new Promise((resolve) => {
183
200
  const sock = net.createConnection({ host, port });
@@ -209,6 +226,11 @@ async function checkInfraHealth() {
209
226
  for (const c of (await projectContainers(INFRA_PROJECT, undefined, { all: false })) ?? []) {
210
227
  services[c.service || c.name] = c.health === "healthy" || (c.state === "running" && !c.health);
211
228
  }
229
+ // Running is not serving: an empty picker mount 404s /__werkmeister on every host (SW-610).
230
+ if (services["caddy"]) {
231
+ const picker = await execCapture(CADDY_CONTAINER, ["test", "-s", PICKER_PAGE]).catch(() => null);
232
+ services["caddy"] = picker?.exitCode === 0;
233
+ }
212
234
  // Fallback: check key ports if the daemon listed nothing
213
235
  if (!Object.keys(services).length) {
214
236
  const portChecks = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "werkmeister-core",
3
- "version": "0.94.2",
3
+ "version": "0.94.3",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "werkmeister's shared library. Its public API is werkmeister-core/sdk; every other entry point is internal.",