werkmeister-core 0.95.4 → 0.95.5

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,13 @@
1
1
  # sandbox-core
2
2
 
3
+ ## 0.95.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 55bc3c0: `with-fixture-workspace` gains `--postmortem` (seeds `postmortem.md`, so `workspace abandon` and `close` run without `--skip-postmortem`), `--worktrees` (each repo a worktree of its source clone on `<user>/demo`, as init makes them, so `pr push` takes it) and `--jira` (a fake Jira for Jira keys, its calls printed as `--- jira calls`). `withFixtureWorkspace` takes `postmortem`, `worktrees` and `jira: true` (calls in `fx.jiraCalls`) (SW-896).
8
+ - 4a8f03e: The fixture workspace's ports (`with-fixture-workspace`, `withFixtureWorkspace`) start at 29100 instead of 39100. 39100 is inside Linux's ephemeral range, so a test server or client socket could land on it and fail the gate fixture's port check with `port 39100 held by a host process` (SW-899).
9
+ - 1b7e7d2: `with-fixture-workspace --workspace <file>` (and `withFixtureWorkspace`'s `workspace` option) now takes only `auto_stop`, `autonomous`, `context_strategy`, `description`, `group`, `issue_id`, `labels`, `model`, `repos`, `starting_prompt`, `task` and `work_type`. Any other key is refused with exit 2, as `--config` already does, since the wrapper is pre-approved and its arguments are its only limit (SW-900).
10
+
3
11
  ## 0.95.4
4
12
 
5
13
  ## 0.95.3
@@ -53,3 +53,9 @@ export interface FakeDaemon {
53
53
  * Any other tool fails with code FAKE_DAEMON, so a path the fake doesn't cover fails loudly.
54
54
  */
55
55
  export declare function startFakeDaemon(tools?: Record<string, unknown>): Promise<FakeDaemon>;
56
+ /**
57
+ * A stand-in for the Jira REST paths jira-rest.ts calls (SW-896): every issue starts `To Do`,
58
+ * `POST …/transitions` moves it, `POST …/comment` is taken. Any other path fails 501, so a path
59
+ * the fake doesn't cover fails loudly. Point JIRA_BASE_URL at `url` with any email and token.
60
+ */
61
+ export declare function startFakeJira(): Promise<FakeDaemon>;
@@ -126,6 +126,67 @@ export async function startFakeDaemon(tools = {}) {
126
126
  }
127
127
  reply(404, { error: `fake daemon: unhandled ${req.method} ${req.url}`, code: "FAKE_DAEMON" });
128
128
  });
129
+ return listening(server, calls);
130
+ }
131
+ const JIRA_STATUSES = ["To Do", "In Progress", "In Review", "Done"];
132
+ /**
133
+ * A stand-in for the Jira REST paths jira-rest.ts calls (SW-896): every issue starts `To Do`,
134
+ * `POST …/transitions` moves it, `POST …/comment` is taken. Any other path fails 501, so a path
135
+ * the fake doesn't cover fails loudly. Point JIRA_BASE_URL at `url` with any email and token.
136
+ */
137
+ export async function startFakeJira() {
138
+ const calls = [];
139
+ const status = new Map();
140
+ const statusOf = (key) => status.get(key) ?? "To Do";
141
+ const issue = (key) => ({
142
+ key,
143
+ fields: {
144
+ summary: "fixture issue",
145
+ status: { name: statusOf(key) },
146
+ issuetype: { name: "Task" },
147
+ comment: { comments: [] },
148
+ },
149
+ });
150
+ const server = http.createServer(async (req, res) => {
151
+ let body = "";
152
+ for await (const chunk of req)
153
+ body += chunk;
154
+ const url = new URL(req.url ?? "/", "http://fake");
155
+ calls.push(`${req.method} ${url.pathname}${body ? ` ${body}` : ""}`);
156
+ const reply = (code, json) => res
157
+ .writeHead(code, { "content-type": "application/json" })
158
+ .end(json === undefined ? "" : JSON.stringify(json));
159
+ const m = /^\/rest\/api\/2\/issue\/([A-Z][A-Z0-9]*-\d+)(\/transitions|\/comment)?$/.exec(url.pathname);
160
+ const key = m?.[1] ?? "";
161
+ if (m && req.method === "GET" && !m[2])
162
+ return reply(200, issue(key));
163
+ if (m?.[2] === "/transitions" && req.method === "GET")
164
+ return reply(200, {
165
+ transitions: JIRA_STATUSES.map((name, i) => ({ id: String(i + 1), name, to: { name } })),
166
+ });
167
+ if (m?.[2] === "/transitions" && req.method === "POST") {
168
+ const id = Number(JSON.parse(body || "{}").transition?.id);
169
+ const to = JIRA_STATUSES[id - 1];
170
+ if (!to)
171
+ return reply(400, { errorMessages: [`fake jira: no transition ${id}`] });
172
+ status.set(key, to);
173
+ return reply(204);
174
+ }
175
+ if (m?.[2] === "/comment" && req.method === "POST")
176
+ return reply(201, {});
177
+ if (req.method === "GET" && url.pathname === "/rest/api/2/search/jql") {
178
+ const keys = /key in \(([^)]*)\)/.exec(url.searchParams.get("jql") ?? "")?.[1] ?? "";
179
+ const found = keys
180
+ .split(",")
181
+ .map((k) => k.trim())
182
+ .filter(Boolean);
183
+ return reply(200, { issues: found.map(issue) });
184
+ }
185
+ reply(501, { errorMessages: [`fake jira: unhandled ${req.method} ${url.pathname}`] });
186
+ });
187
+ return listening(server, calls);
188
+ }
189
+ async function listening(server, calls) {
129
190
  await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
130
191
  const { port } = server.address();
131
192
  return {
@@ -61,6 +61,12 @@ export interface FixtureOptions {
61
61
  * `since` is that many minutes back (and `services`, a service start), so admission prices and ages it (SW-809).
62
62
  */
63
63
  queue?: string[];
64
+ /** A `postmortem.md` at the workspace root, on the record branch, so close-out runs without `--skip-postmortem` (SW-896). */
65
+ postmortem?: boolean;
66
+ /** Each repo a worktree of its source clone, as init makes them, on `<os user>/demo`, so `pr push` takes it (SW-896). */
67
+ worktrees?: boolean;
68
+ /** JIRA_BASE_URL for the fixture (a fake Jira's URL, `startFakeJira`). Default: blank Jira credentials. */
69
+ jira?: string;
64
70
  }
65
71
  /** One running container as `docker inspect` reports it to the scheduler. */
66
72
  export interface FixtureContainer {
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { execFileSync } from "node:child_process";
8
8
  import * as fs from "node:fs";
9
+ import * as os from "node:os";
9
10
  import * as path from "node:path";
10
11
  import YAML from "yaml";
11
12
  import { waitingPath } from "./agent_state.js";
@@ -20,6 +21,22 @@ const NAME = /^[a-z0-9][a-z0-9-]*$/;
20
21
  const QUEUE_ENTRY = /^([a-z0-9][a-z0-9-]*)(?::(\d+)(?::([a-z0-9][a-z0-9-]*(?:,[a-z0-9][a-z0-9-]*)*))?)?$/;
21
22
  // The only config.yaml keys --config may add: others (jira, github_ingest, paths…) reach real services or repos from a pre-approved run.
22
23
  const CONFIG_KEYS = ["budget", "workspace_ask_rules"];
24
+ // The only workspace.yml keys --workspace may set (SW-900): text and flags the fixture's own files take, and
25
+ // `repos`, whose names init keeps under repos_root. Tracker refs reach the fake gh or blank/fake Jira creds.
26
+ const WORKSPACE_KEYS = [
27
+ "auto_stop",
28
+ "autonomous",
29
+ "context_strategy",
30
+ "description",
31
+ "group",
32
+ "issue_id",
33
+ "labels",
34
+ "model",
35
+ "repos",
36
+ "starting_prompt",
37
+ "task",
38
+ "work_type",
39
+ ];
23
40
  export const FIXTURE_FEATURE = "demo";
24
41
  export const FIXTURE_BRANCH = "me/demo";
25
42
  export const FIXTURE_REPO_SLUG = "o/app";
@@ -46,10 +63,13 @@ repo:
46
63
  packages/lib: lib
47
64
  `;
48
65
  const FRAGMENT_FILE = path.join(".werkmeister", "services.yaml");
49
- // High base_port, so a real `up` from a fixture (SW-284) can't clash with real workspaces' slots.
66
+ // Above real workspaces' slots, so a real `up` from a fixture (SW-284) can't clash, and below every
67
+ // OS's ephemeral range (Linux 32768+) and k8s NodePorts (30000+), where listen(0) servers and client
68
+ // sockets land: one on the gate's port probe read as "held by a host process" (SW-899).
69
+ // shortcut: every fixture shares this block, so two parallel gate probes can still meet for under a ms; give each fixture its own block if that flakes.
50
70
  const PORTS = `feature_slots:
51
71
  count: 4
52
- base_port: 39100
72
+ base_port: 29100
53
73
  ports_per_slot: 20
54
74
  `;
55
75
  const FIXTURE_PROJECT = "fixture";
@@ -81,6 +101,9 @@ export function fixturePreset({ gate, bare, change, ...opts }) {
81
101
  const refused = Object.keys(opts.config ?? {}).filter((k) => !CONFIG_KEYS.includes(k));
82
102
  if (refused.length)
83
103
  throw new Error(`--config may not set ${refused.join(", ")}: only ${CONFIG_KEYS.join(", ")}`);
104
+ const unlisted = Object.keys(opts.workspace ?? {}).filter((k) => !WORKSPACE_KEYS.includes(k));
105
+ if (unlisted.length)
106
+ throw new Error(`--workspace may not set ${unlisted.join(", ")}: only ${WORKSPACE_KEYS.join(", ")}`);
84
107
  for (const c of opts.containers ?? []) {
85
108
  const bad = !NAME.test(c.feature)
86
109
  ? c.feature
@@ -95,6 +118,9 @@ export function fixturePreset({ gate, bare, change, ...opts }) {
95
118
  const badEntry = opts.queue?.find((q) => !QUEUE_ENTRY.test(q));
96
119
  if (badEntry !== undefined)
97
120
  throw new Error(`--queue: bad entry ${JSON.stringify(badEntry)}: <feature>[:<waitedMin>[:<service>,...]], lowercase names and whole minutes`);
121
+ // A worktree shares its source's origin/main, so it can't stay behind until a fetch.
122
+ if (opts.worktrees && opts.behind)
123
+ throw new Error("--worktrees and --behind don't combine");
98
124
  const libChange = "packages/lib/index.js";
99
125
  const gateChange = change ?? (opts.fragmentChange === undefined ? libChange : undefined);
100
126
  if (gate && opts.services && gateChange === libChange) {
@@ -161,6 +187,8 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
161
187
  const extra = (Object.keys(prFields).length ? YAML.stringify(prFields) : "") +
162
188
  (opts.prLines ?? []).map((l) => `${l}\n`).join("");
163
189
  let applyFragmentChange;
190
+ // pr push takes only the workspace's own <os user>/<workspace> branch (pushOne); the clone keeps me/demo.
191
+ const branch = opts.worktrees ? `${os.userInfo().username}/${FIXTURE_FEATURE}` : FIXTURE_BRANCH;
164
192
  for (const { name, change } of repos) {
165
193
  // Ends in owner/repo.git so originSlug matches the PR file's repo: (SW-381).
166
194
  const remote = path.join(root, "remotes", "o", `${name}.git`);
@@ -176,7 +204,11 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
176
204
  if (opts.fragment && name === repos[0]?.name)
177
205
  write(path.join(source, FRAGMENT_FILE), opts.fragment);
178
206
  const repo = path.join(workspace, name);
179
- git(workspace, "clone", "-q", remote, name);
207
+ // Detached: the source still has main checked out until it moves to fixture-source below.
208
+ if (opts.worktrees)
209
+ git(source, "worktree", "add", "-q", "--detach", repo);
210
+ else
211
+ git(workspace, "clone", "-q", remote, name);
180
212
  const frag = name === repos[0]?.name ? opts.fragmentChange : undefined;
181
213
  const changed = [change, frag === undefined ? undefined : FRAGMENT_FILE].filter(Boolean);
182
214
  if (changed.length) {
@@ -186,9 +218,10 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
186
218
  write(path.join(repo, FRAGMENT_FILE), opts.fragment ?? BASE_FRAGMENT);
187
219
  git(repo, "add", ".");
188
220
  git(repo, "commit", "-q", "-m", "base");
189
- git(repo, "push", "-q", "origin", "main");
221
+ // HEAD:main, since a detached worktree's `main` is the source's, still at init.
222
+ git(repo, "push", "-q", "origin", "HEAD:main");
190
223
  }
191
- git(repo, "switch", "-q", "-c", FIXTURE_BRANCH);
224
+ git(repo, "switch", "-q", "-c", branch);
192
225
  if (opts.behind) {
193
226
  // Pushed from the source clone, so the workspace clone's origin/main stays stale until a fetch.
194
227
  git(source, "fetch", "-q", "origin", "main");
@@ -211,10 +244,12 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
211
244
  git(repo, "commit", "-q", "-am", `change ${FRAGMENT_FILE}`);
212
245
  };
213
246
  if (opts.prFile && changed.length)
214
- write(path.join(notes, `pr-${name}.md`), `---\nrepo: o/${name}\nbranch: ${FIXTURE_BRANCH}\nbase: main\nstatus: ${status}\n${extra}---\n# feat: fixture change\n\n${PASS}`);
247
+ write(path.join(notes, `pr-${name}.md`), `---\nrepo: o/${name}\nbranch: ${branch}\nbase: main\nstatus: ${status}\n${extra}---\n# feat: fixture change\n\n${PASS}`);
215
248
  }
216
249
  if (opts.prFile)
217
250
  write(path.join(notes, "exercise.md"), PASS);
251
+ if (opts.postmortem)
252
+ write(path.join(workspace, "postmortem.md"), "# Post-mortem\n\nseeded by the fixture\n");
218
253
  // The workspace folder is a git repo with its own local origin, like a real one, so `pr open`
219
254
  // commits and pushes the record (its code clones are ignored).
220
255
  const record = path.join(root, "remotes", "o", "ws.git");
@@ -299,10 +334,11 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
299
334
  // dockerode honours DOCKER_HOST before any context, so no engine call reaches the host (SW-164).
300
335
  // A fixed short path: one under a deep temp root passes macOS's 104-byte socket limit and crashes with EINVAL.
301
336
  DOCKER_HOST: "unix:///nonexistent/werkmeister-fixture-docker.sock",
302
- // jiraCreds() reads these before config; blank, the fixture config's missing `jira:` wins.
303
- JIRA_BASE_URL: "",
304
- JIRA_EMAIL: "",
305
- JIRA_API_TOKEN: "",
337
+ // jiraCreds() reads these before config; blank, the fixture config's missing `jira:` wins, so
338
+ // only the fake Jira (`jira`) is ever reached.
339
+ JIRA_BASE_URL: opts.jira ?? "",
340
+ JIRA_EMAIL: opts.jira ? "fixture@example.invalid" : "",
341
+ JIRA_API_TOKEN: opts.jira ? "fixture" : "",
306
342
  // A fixture step must not act as the caller on Anthropic, a cluster or GitHub (SW-424).
307
343
  ANTHROPIC_API_KEY: "",
308
344
  CLAUDE_CODE_OAUTH_TOKEN: "",
@@ -330,7 +366,7 @@ const SCRIPT = `#!/bin/sh
330
366
  # Generated by \`werkmeister workspace init\` (SW-292); rerun it to regenerate.
331
367
  # Runs this workspace's own built werkmeister CLI against a seeded fixture workspace, removed on exit:
332
368
  # temp HOME and registry, a local bare origin, logging fakes for gh and docker, dead Docker and daemon.
333
- # usage: with-fixture-workspace [--no-wait] [--gate] [--bare] [--daemon] [--exec-runs] [--merged] [--behind] [--slots-full] [--running <file>] [--queue <feature>[,<feature>]... | --queue <feature>:<waitedMin>[:<service>,...]]... [--issue <ref>] [--pr-field <key>=<value>]... [--pr-line '<key>: <value>']... [--workspace <file>] [--config <file>] [--services <file>] [--fragment <file>] [--change <path>] [--change-base <file>] [--change-branch <file>] [--fragment-change <file>] [--stale-compose] [--print <file>]... <werkmeister args> [--then <werkmeister args>]...
369
+ # usage: with-fixture-workspace [--no-wait] [--gate] [--bare] [--daemon] [--exec-runs] [--merged] [--behind] [--slots-full] [--postmortem] [--worktrees] [--jira] [--running <file>] [--queue <feature>[,<feature>]... | --queue <feature>:<waitedMin>[:<service>,...]]... [--issue <ref>] [--pr-field <key>=<value>]... [--pr-line '<key>: <value>']... [--workspace <file>] [--config <file>] [--services <file>] [--fragment <file>] [--change <path>] [--change-base <file>] [--change-branch <file>] [--fragment-change <file>] [--stale-compose] [--print <file>]... <werkmeister args> [--then <werkmeister args>]...
334
370
  # It only ever runs that CLI, never another command, so one allow rule can cover it (SW-172, SW-231);
335
371
  # --exec-runs, given first, also runs the registry's rows on the host, so the workspace asks before it (SW-473).
336
372
  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.95.4",
3
+ "version": "0.95.5",
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.",