werkmeister-core 0.90.2 → 0.90.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.90.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 4941f67: `with-fixture-workspace` takes `--running <file>` and `--queue <feature>[,<feature>]...`, so a capacity wait runs through the built CLI. Each `--running` line (`feature<TAB>workspace<TAB>startedAt<TAB>memoryBytes<TAB>nanoCpus`) is a container the fixture's fake `docker ps -q` / `docker inspect` show the scheduler as running; the workspace is a folder name created empty in the fixture, so the holder is a person's and never preempted. `--queue` seeds `scheduler/queue.json`. With `--config` `budget: { cpus: 6 }` and a 6-CPU holder, `feature up` exits 75 naming the holder, and `budget status` lists it under Running. `withFixtureWorkspace` takes the same as `containers` and `queue`.
8
+ - b61338b: `with-fixture-workspace` and `withFixtureWorkspace` point the dead daemon address (`WERKMEISTER_HOST`) at a port just bound and closed, instead of `127.0.0.1:1`, which fetch rejects as a "bad port" (a TypeError with no `cause.code`) before connecting. A fixture run now sees a refused connection (`ECONNREFUSED`), as with a real stopped daemon, so the refused, restart and fallback paths can be exercised through the fixture.
9
+
3
10
  ## 0.90.2
4
11
 
5
12
  ### Patch Changes
@@ -26,6 +26,8 @@ export interface FakeWorld {
26
26
  rules?: unknown[];
27
27
  /** Containers the fake docker shows as running, to the gate's `ps --format {{.Names}}…` only. */
28
28
  running?: string[];
29
+ /** `docker inspect` lines (`feature\tworkdir\tstartedAt\tmemory\tnanoCpus`) the scheduler reads as running features (SW-685). */
30
+ containers?: string[];
29
31
  /** Set: `docker exec` runs its last argument (the gate row's command) with `sh -c` here and exits with its code. Absent: exit 0. */
30
32
  execCwd?: string;
31
33
  }
@@ -10,7 +10,8 @@ import * as path from "node:path";
10
10
  // Unknown verbs exit 1, so a code path a fake doesn't cover fails loudly (SW-148).
11
11
  // `pr list` prints nothing: pr open's --jq leaves no URL when no PR is open.
12
12
  // Only the gate's `ps --format {{.Names}}\t{{.State}}` sees running containers; every other `ps`
13
- // (-q/-aq by label) finds none, so no teardown of a pre-rename project runs on a fake hit.
13
+ // (-q/-aq by label) finds none, so no teardown of a pre-rename project runs on a fake hit. Only the
14
+ // scheduler's exact feature-label `ps -q` and its NanoCpus `inspect` see `containers` (SW-685).
14
15
  const FAKE_JS = `import { spawnSync } from "node:child_process";
15
16
  import * as fs from "node:fs";
16
17
  const [, , tool, dir, ...args] = process.argv;
@@ -66,8 +67,11 @@ if (tool === "gh") {
66
67
  switch (args[0]) {
67
68
  case "ps":
68
69
  if (args.some((a) => a.includes("{{.Names}}"))) out((world.running ?? []).map((c) => c + "\\trunning\\n").join(""));
70
+ else if (args.join(" ") === "ps -q --filter label=com.werkmeister.feature") out((world.containers ?? []).map((_, i) => "c" + i + "\\n").join(""));
71
+ break;
72
+ case "inspect":
73
+ out(args[1] === "--format" && args[2].includes("NanoCpus") ? (world.containers ?? []).map((l) => l + "\\n").join("") : "false\\n");
69
74
  break;
70
- case "inspect": out("false\\n"); break;
71
75
  case "exec":
72
76
  if (world.execCwd) process.exit(spawnSync("sh", ["-c", args.at(-1)], { cwd: world.execCwd, stdio: "inherit" }).status ?? 1);
73
77
  break;
@@ -49,6 +49,20 @@ export interface FixtureOptions {
49
49
  changeBranch?: string;
50
50
  /** Top-level keys added to the fixture's config.yaml, e.g. `{ budget: { host_load: 0.01 } }` (SW-670); fixturePreset refuses any key but CONFIG_KEYS. */
51
51
  config?: Record<string, unknown>;
52
+ /** Containers the fake docker shows the scheduler as running, so a start can wait for capacity (SW-685). */
53
+ containers?: FixtureContainer[];
54
+ /** Features in the waiting queue, `scheduler/queue.json` (SW-685). */
55
+ queue?: string[];
56
+ }
57
+ /** One running container as `docker inspect` reports it to the scheduler. */
58
+ export interface FixtureContainer {
59
+ feature: string;
60
+ /** A folder name, created empty under the fixture's workspaces root: a person's workspace, never preempted. */
61
+ workspace: string;
62
+ startedAt: string;
63
+ /** Bytes reserved. */
64
+ memory: number;
65
+ nanoCpus: number;
52
66
  }
53
67
  export interface FixturePresets {
54
68
  /** An approved PR for a one-file change in packages/lib, with `demo-lib` running, for `pr gate`. */
@@ -13,6 +13,8 @@ import { dotDir } from "./dot_dir.js";
13
13
  import { writeFakes } from "./fixture_fakes.js";
14
14
  import { gitEnv } from "./proc.js";
15
15
  import { prDir, stageDirs, templateFor } from "./strategies/icm_templates.js";
16
+ // Names become folders under the fixture and lines in the fakes, from a pre-approved wrapper's input.
17
+ const NAME = /^[a-z0-9][a-z0-9-]*$/;
16
18
  // The only config.yaml keys --config may add: others (jira, github_ingest, paths…) reach real services or repos from a pre-approved run.
17
19
  const CONFIG_KEYS = ["budget"];
18
20
  export const FIXTURE_FEATURE = "demo";
@@ -76,6 +78,20 @@ export function fixturePreset({ gate, bare, change, ...opts }) {
76
78
  const refused = Object.keys(opts.config ?? {}).filter((k) => !CONFIG_KEYS.includes(k));
77
79
  if (refused.length)
78
80
  throw new Error(`--config may not set ${refused.join(", ")}: only ${CONFIG_KEYS.join(", ")}`);
81
+ for (const c of opts.containers ?? []) {
82
+ const bad = !NAME.test(c.feature)
83
+ ? c.feature
84
+ : !NAME.test(c.workspace)
85
+ ? c.workspace
86
+ : Number.isNaN(Date.parse(c.startedAt))
87
+ ? c.startedAt
88
+ : [c.memory, c.nanoCpus].find((n) => !Number.isSafeInteger(n) || n < 0);
89
+ if (bad !== undefined)
90
+ throw new Error(`--running: bad value ${JSON.stringify(bad)}: feature and workspace are lowercase names, startedAt a date, memory and nanoCpus whole numbers`);
91
+ }
92
+ const badName = opts.queue?.find((f) => !NAME.test(f));
93
+ if (badName !== undefined)
94
+ throw new Error(`--queue: bad feature ${JSON.stringify(badName)}: lowercase names, comma-separated`);
79
95
  const libChange = "packages/lib/index.js";
80
96
  const gateChange = change ?? (opts.fragmentChange === undefined ? libChange : undefined);
81
97
  if (gate && opts.services && gateChange === libChange) {
@@ -207,11 +223,19 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
207
223
  // leasesPath() beside configFile: the env that points it here isn't set yet in this process.
208
224
  write(path.join(path.dirname(configFile), "scheduler", "leases.json"), JSON.stringify(leases));
209
225
  }
226
+ if (opts.queue)
227
+ write(path.join(path.dirname(configFile), "scheduler", "queue.json"), JSON.stringify(opts.queue));
228
+ const containers = (opts.containers ?? []).map((c) => {
229
+ const ws = path.join(root, "ws", c.workspace);
230
+ fs.mkdirSync(ws, { recursive: true });
231
+ return [c.feature, ws, c.startedAt, c.memory, c.nanoCpus].join("\t");
232
+ });
210
233
  const home = path.join(root, "home");
211
234
  fs.mkdirSync(home);
212
235
  const fakes = writeFakes(path.join(root, "bin"), {
213
236
  prUrl: `https://github.com/${FIXTURE_REPO_SLUG}/pull/1`,
214
237
  running: opts.running ?? [],
238
+ containers,
215
239
  prState: opts.merged ? "MERGED" : undefined,
216
240
  issue: yml["issue_id"] ? FIXTURE_ISSUE : undefined,
217
241
  // shortcut: every exec runs in the first code repo; map container → repo when a fixture has two.
@@ -247,15 +271,27 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
247
271
  GH_TOKEN: "",
248
272
  GITHUB_TOKEN: "",
249
273
  // Dead even when the caller's shell exports the real daemon's address (SW-264, SW-426).
250
- WERKMEISTER_HOST: opts.daemon ?? "http://127.0.0.1:1",
274
+ WERKMEISTER_HOST: opts.daemon ?? closedPortUrl(),
275
+ // A refused daemon is retried for a restart gap (15 s default): each fixture step would wait it out (SW-708).
276
+ WERKMEISTER_DAEMON_RETRY_MS: process.env["WERKMEISTER_DAEMON_RETRY_MS"] || "500",
251
277
  },
252
278
  };
253
279
  }
280
+ // A port just bound and closed, so a call is refused (ECONNREFUSED) like a stopped daemon: fetch
281
+ // rejects port 1 as a "bad port" before connecting (SW-708). Sync, since seedFixture is.
282
+ // shortcut: another process can take the port between close and use; hold a socket if that flakes.
283
+ function closedPortUrl() {
284
+ const port = execFileSync(process.execPath, [
285
+ "-e",
286
+ "const s=require('net').createServer().listen(0,'127.0.0.1',()=>{process.stdout.write(String(s.address().port));s.close()})",
287
+ ]).toString();
288
+ return `http://127.0.0.1:${port}`;
289
+ }
254
290
  const SCRIPT = `#!/bin/sh
255
291
  # Generated by \`werkmeister workspace init\` (SW-292); rerun it to regenerate.
256
292
  # Runs this workspace's own built werkmeister CLI against a seeded fixture workspace, removed on exit:
257
293
  # temp HOME and registry, a local bare origin, logging fakes for gh and docker, dead Docker and daemon.
258
- # usage: with-fixture-workspace [--no-wait] [--gate] [--bare] [--daemon] [--exec-runs] [--merged] [--behind] [--slots-full] [--issue <ref>] [--pr-field <key>=<value>]... [--workspace <file>] [--config <file>] [--services <file>] [--fragment <file>] [--change <path>] [--change-base <file>] [--change-branch <file>] [--fragment-change <file>] [--print <file>]... <werkmeister args> [--then <werkmeister args>]...
294
+ # usage: with-fixture-workspace [--no-wait] [--gate] [--bare] [--daemon] [--exec-runs] [--merged] [--behind] [--slots-full] [--running <file>] [--queue <feature>[,<feature>]...] [--issue <ref>] [--pr-field <key>=<value>]... [--workspace <file>] [--config <file>] [--services <file>] [--fragment <file>] [--change <path>] [--change-base <file>] [--change-branch <file>] [--fragment-change <file>] [--print <file>]... <werkmeister args> [--then <werkmeister args>]...
259
295
  # It only ever runs that CLI, never another command, so one allow rule can cover it (SW-172, SW-231);
260
296
  # --exec-runs, given first, also runs the registry's rows on the host, so the workspace asks before it (SW-473).
261
297
  ws=$(cd "$(dirname "$0")/../.." && pwd -P) || exit 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "werkmeister-core",
3
- "version": "0.90.2",
3
+ "version": "0.90.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.",