werkmeister-core 0.95.1 → 0.95.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,33 @@
1
1
  # sandbox-core
2
2
 
3
+ ## 0.95.3
4
+
5
+ ### Patch Changes
6
+
7
+ - f6bfd16: When werkmeister stops a workspace on its own (preempted, or idle), it records why in `.werkmeister/last-stop.json`, and the "Capacity freed" nudge that brings the session back says who stopped it and which commands that killed (SW-702).
8
+ - 27d4ef6: A feature with a `docker exec` running in its containers (a session's test run, not only the gate's) is busy: preemption never picks it and the idle reaper never stops it. `budget status` shows it as `run in flight`. When werkmeister does stop a workspace on its own (preempted, or idle), its session's nudge names each command the stop killed and says to rerun it (SW-856).
9
+
10
+ `with-fixture-workspace --running` takes an optional 6th column, the running `docker exec`s, so an exercise can show a busy holder.
11
+
12
+ - 0270f6f: An allocate inside a workspace container no longer prunes the other workspaces' leases: it sees only its own folder, so every other lease looked gone. Leases are pruned from the host only (SW-860).
13
+ - 8977cdb: A start that fails in compose now says `werkmeister feature logs <feature>` shows why, not "see the compose output above": when the grant starts a waiter, that output goes to another process, and the session's nudge carried the dead pointer (SW-861).
14
+ - 740bdaa: `feature up` leaves the waiting queue and clears `waiting.json` once its containers start. An admit that returned early (Docker unreadable, or a zero-cost start) left both, so the hook kept saying "queued" over a running feature (SW-863).
15
+ - ddc7d5f: The services `pr gate` and `pr open` start now share one 30-minute wait for room in the budget, keeping their queue place, instead of 10 minutes each that ended by dropping the place (SW-869).
16
+ - 7c27198: The grant pass no longer stops a parked `--service` waiter's own running containers: with no wait it could not park again, so the workspace was left with nothing running and no queue place. Any other way a parked waiter leaves the queue now tells its session why (SW-873).
17
+
18
+ ## 0.95.2
19
+
20
+ ### Patch Changes
21
+
22
+ - 27d6925: Autonomous workspaces narrow two CLI rules that reached past the workspace. `werkmeister pr:*` becomes `pr gate|open|status|new|adopt`, so `pr watch` (whose `--install` writes a launchd job) asks. `workspace init:*`/`init-workspace:*` become the bare form plus `--refresh-issue`, `--prune-context` and `--labels focus`, so `--feature <other>`, `--repos`, `--force` and `--autonomous` ask. Autonomous mode also denies Edit on `.env*`, `.werkmeister/workspace.yml`, `.werkmeister/claude-permissions.json` and `.werkmeister/token`, which the next wake's rules are read from (SW-625).
23
+ - cf3f368: Autonomous workspaces no longer pre-approve `Bash(git:*)`. They get an allow-list of the subcommands a run uses (status, diff, log, show, add, commit, restore, branch, rev-parse, ls-files, merge-base, rev-list, switch, checkout, symbolic-ref, `worktree list`), bare or as `git -C <repo>` for the workspace's own repos only, plus exact `fetch`, `fetch origin`, `fetch origin main`, `remote -v`, `remote get-url origin` and `rebase origin/main`/`--continue`/`--abort`. `grep` (whose pager option git also takes as `-lO` or `--open`), `rebase -x`, `bisect run`, `submodule foreach`, `difftool`, `push`, `init`, `clone`, `stash` and `-C <another workspace>` now ask; `--output` and `--no-index` (which write and read any path) and Edit on any `.git/` are denied (SW-626).
24
+ - 7fccb55: Rerunning `werkmeister workspace init` on an existing workspace now commits the regenerated contracts (CLAUDE.md, CONTEXT.md, `_shared/`, each stage's CONTEXT.md) on the record branch as their own commit, `chore: regenerate workspace contracts`, so they no longer ride along with the agent's next commit. Stage outputs and notes are left alone. A live session gets a nudge to re-read its contracts (SW-760).
25
+ - a8c988f: Run limits now allow `werkmeister issue create --relates <ticket>` for follow-ups a run finds, and `werkmeister feature up` when the exercise or a measurement needs the container, not only the gate. An autonomous workspace gets the matching allow rule, `issue create --relates <its issue>`, so a headless run files without a prompt; in an autonomous workspace `issue create` files only in its own issue's project and posts only a `--body-file` inside the workspace (no stdin, no path out of it) (SW-815).
26
+ - c8901fa: The reproduce contract's permission-rule step now also asks for a `git grep -F` of the exact old rule text across the whole repo, tests and `apps/` included, with each hit listed as a site the fix must update (SW-820).
27
+ - f643c98: Autonomous workspaces no longer pre-approve starting or restarting a service whose mounts give it the docker socket, the werkmeister home, the workspaces or repos root (or a parent), another workspace or a checkout under those roots (`feature up --service <svc>`, `docker restart <feature>-<svc>`), nor a whole-feature `feature up`/`restart` while such a service is selected: its `command:` often runs a package script the agent can edit. A new registry field, `unattended_start: <reason>`, keeps the rules for a service shown to run no such script. Only the machine registry's value counts, and only while the branch's fragment leaves the service's `command`, `image`, `build`, `entrypoint`, `archetype`, mounts and `environment` as the registry has them (SW-854).
28
+ - 0a604fa: Autonomous workspaces no longer pre-approve `werkmeister feature test|logs|services <feature>:*`. The prefix let an agent run `feature test <feature> -w <other> --scaffold`, which writes another workspace, and `feature test <feature> -- --config=<file>`, which runs any JS through Playwright. `feature services <feature>` and `feature logs <feature>` (with or without one of the workspace's own services) are now exact, plus their pre-SW-186 spellings. `feature test` has no rule and asks: even exact, it runs the workspace's own `playwright.config.*`, which the agent can write, with the real HOME. The threat model now records the `host.docker.internal` probe: a container has no network route to Docker, but it does reach the werkmeister daemon with its workspace's token (SW-855).
29
+ - ca7c8d2: `werkmeister-context.md` § Test commands now counts the mounts a service gets from the branch's own `.werkmeister/services.yaml` and the workspace's `dev_links`, as the allow rules already did. A service whose only `~/.werkmeister` or docker-socket mount comes from `dev_links` now prints its test under `with-temp-home` instead of a bare `docker exec` (SW-872).
30
+
3
31
  ## 0.95.1
4
32
 
5
33
  ### Patch Changes
@@ -122,6 +122,15 @@ export declare function readWaiting(root: string): WaitingFile | null;
122
122
  export declare function liveWaiting(root: string): WaitingFile | null;
123
123
  export declare function writeWaiting(root: string, w: WaitingFile): void;
124
124
  export declare function clearWaiting(root: string): void;
125
+ /** Why werkmeister last stopped this workspace on its own (SW-702): preemption or the idle reaper. */
126
+ export interface LastStop {
127
+ at: string;
128
+ why: string;
129
+ killed: string[];
130
+ }
131
+ export declare function writeLastStop(root: string, stop: LastStop): void;
132
+ /** Read and remove the last stop, so one grant reports it once. */
133
+ export declare function claimLastStop(root: string): LastStop | null;
125
134
  /** The pending "continue" for a live session, left by Resume for the pane to deliver. */
126
135
  export declare function readNudge(root: string): string | null;
127
136
  export declare function writeNudge(root: string, message: string, now?: Date): void;
@@ -449,6 +449,27 @@ export function writeWaiting(root, w) {
449
449
  export function clearWaiting(root) {
450
450
  fs.rmSync(waitingPath(root), { force: true });
451
451
  }
452
+ const lastStopPath = (root) => path.join(dotDir(root), "last-stop.json");
453
+ export function writeLastStop(root, stop) {
454
+ writeAtomic(lastStopPath(root), stop);
455
+ }
456
+ /** Read and remove the last stop, so one grant reports it once. */
457
+ export function claimLastStop(root) {
458
+ try {
459
+ const o = JSON.parse(fs.readFileSync(lastStopPath(root), "utf8"));
460
+ fs.rmSync(lastStopPath(root), { force: true });
461
+ if (typeof o?.why !== "string")
462
+ return null;
463
+ return {
464
+ at: String(o.at),
465
+ why: o.why,
466
+ killed: Array.isArray(o.killed) ? o.killed.map(String) : [],
467
+ };
468
+ }
469
+ catch {
470
+ return null;
471
+ }
472
+ }
452
473
  /** The pending "continue" for a live session, left by Resume for the pane to deliver. */
453
474
  export function readNudge(root) {
454
475
  try {
@@ -15,10 +15,13 @@
15
15
  */
16
16
  import { type Archetype, type ArchetypeRegistry, type ReposConfig, type ServiceEntry, type ServicesConfig } from "./config.js";
17
17
  /**
18
- * True when one of *entry*'s host binds is a docker socket (any path ending `docker.sock`, e.g. OrbStack's)
19
- * or the werkmeister home, or a parent of either.
18
+ * True when one of *entry*'s host binds is a docker socket (any path ending `docker.sock`, e.g. OrbStack's),
19
+ * or the werkmeister home, the workspaces root or the repos root, or a parent of one. With *workspaceRoot*,
20
+ * also anything else under those roots: another workspace, or a checkout a dev-linked CLI runs from.
21
+ * SW-855: complete for docker (no TCP API on host.docker.internal), partial for the werkmeister daemon,
22
+ * which any container holding the workspace's token reaches on :3002 (docs/context-strategies.md).
20
23
  */
21
- export declare function hostAccess(entry: ServiceEntry | undefined): boolean;
24
+ export declare function hostAccess(entry: ServiceEntry | undefined, workspaceRoot?: string): boolean;
22
25
  /** True when *entry*'s container sees host path *p* at the same path (one of its binds covers it). */
23
26
  export declare function seesHostPath(entry: ServiceEntry | undefined, p: string): boolean;
24
27
  /**
@@ -115,3 +118,10 @@ export declare function presentArchetypes(repos: Array<[string, string]>, meta:
115
118
  * at workspace time rather than silently resolving.
116
119
  */
117
120
  export declare function validateServiceArchetypes(servicesConfig: ServicesConfig, archetypes: ArchetypeRegistry): string[];
121
+ /**
122
+ * SW-845: *registry* with each entry's `extra_volumes` widened to every bind compose mounts: the
123
+ * branch's own fragment and the workspace's `dev_links` too (both agent-editable), variables
124
+ * substituted as generate.ts does. Only the mounts: test commands stay the registry's.
125
+ * *branch* is registry_sync.ts's `workspaceServices(registry, workspaceRoot)`; importing it here is a cycle.
126
+ */
127
+ export declare function withComposeMounts(registry: ServicesConfig, workspaceRoot: string, branch: ServicesConfig): ServicesConfig;
@@ -15,7 +15,8 @@
15
15
  */
16
16
  import * as fs from "node:fs";
17
17
  import * as path from "node:path";
18
- import { expandUser, loadArchetypes, sandboxHome, } from "./config.js";
18
+ import { parse as parseYaml } from "yaml";
19
+ import { dotDir, expandUser, getReposRoot, loadArchetypes, sandboxHome, sandboxRoot, workspacesRoot, } from "./config.js";
19
20
  const realPath = (p) => {
20
21
  try {
21
22
  return fs.realpathSync(p);
@@ -31,12 +32,20 @@ const hostBinds = (entry) => (entry?.extra_volumes ?? [])
31
32
  .map((src) => realPath(expandUser(src)));
32
33
  const within = (p, dir) => p === dir || p.startsWith(`${dir}${path.sep}`) || dir === "/";
33
34
  /**
34
- * True when one of *entry*'s host binds is a docker socket (any path ending `docker.sock`, e.g. OrbStack's)
35
- * or the werkmeister home, or a parent of either.
35
+ * True when one of *entry*'s host binds is a docker socket (any path ending `docker.sock`, e.g. OrbStack's),
36
+ * or the werkmeister home, the workspaces root or the repos root, or a parent of one. With *workspaceRoot*,
37
+ * also anything else under those roots: another workspace, or a checkout a dev-linked CLI runs from.
38
+ * SW-855: complete for docker (no TCP API on host.docker.internal), partial for the werkmeister daemon,
39
+ * which any container holding the workspace's token reaches on :3002 (docs/context-strategies.md).
36
40
  */
37
- export function hostAccess(entry) {
38
- const targets = ["/var/run/docker.sock", sandboxHome()].map(realPath);
39
- return hostBinds(entry).some((host) => path.basename(host) === "docker.sock" || targets.some((t) => within(t, host)));
41
+ export function hostAccess(entry, workspaceRoot) {
42
+ const roots = [workspacesRoot(), getReposRoot()].map(realPath);
43
+ const targets = ["/var/run/docker.sock", sandboxHome(), ...roots].map(realPath);
44
+ // Both roots default to ~/workspace, so the workspace's own mount is under them.
45
+ const own = workspaceRoot && realPath(workspaceRoot);
46
+ return hostBinds(entry).some((host) => path.basename(host) === "docker.sock" ||
47
+ targets.some((t) => within(t, host)) ||
48
+ Boolean(own && roots.some((r) => within(host, r)) && !within(host, own)));
40
49
  }
41
50
  /** True when *entry*'s container sees host path *p* at the same path (one of its binds covers it). */
42
51
  export function seesHostPath(entry, p) {
@@ -260,3 +269,34 @@ export function validateServiceArchetypes(servicesConfig, archetypes) {
260
269
  }
261
270
  return errors;
262
271
  }
272
+ /**
273
+ * SW-845: *registry* with each entry's `extra_volumes` widened to every bind compose mounts: the
274
+ * branch's own fragment and the workspace's `dev_links` too (both agent-editable), variables
275
+ * substituted as generate.ts does. Only the mounts: test commands stay the registry's.
276
+ * *branch* is registry_sync.ts's `workspaceServices(registry, workspaceRoot)`; importing it here is a cycle.
277
+ */
278
+ export function withComposeMounts(registry, workspaceRoot, branch) {
279
+ // generate.ts's loadDevLinks reads the same field, but generate imports this module.
280
+ let devLinks = {};
281
+ try {
282
+ const yml = parseYaml(fs.readFileSync(path.join(dotDir(workspaceRoot), "workspace.yml"), "utf8"));
283
+ devLinks = yml?.["dev_links"] ?? {};
284
+ }
285
+ catch {
286
+ // No workspace.yml (or unreadable): no dev_links, as generate.ts reads it.
287
+ }
288
+ return Object.fromEntries(Object.entries(registry).map(([id, entry]) => {
289
+ const repo = path.join(workspaceRoot, String(branch[id]?.["repo"] ?? entry["repo"] ?? ""));
290
+ const specs = [
291
+ ...(entry.extra_volumes ?? []),
292
+ ...(branch[id]?.extra_volumes ?? []),
293
+ ...(devLinks[id] ?? []),
294
+ ].map((s) => {
295
+ const out = s.split("${WORKSPACE}").join(workspaceRoot).split("${REPO}").join(repo);
296
+ return out.includes("${WERKMEISTER_ROOT}")
297
+ ? out.split("${WERKMEISTER_ROOT}").join(sandboxRoot())
298
+ : out;
299
+ });
300
+ return [id, { ...entry, extra_volumes: specs }];
301
+ }));
302
+ }
package/dist/docker.d.ts CHANGED
@@ -180,6 +180,11 @@ export declare function containerLogs(project: string, service: string | null, {
180
180
  * single entry is renamed to the destination's basename.
181
181
  */
182
182
  export declare function copyFileToContainer(container: string, hostPath: string, containerPath: string): Promise<void>;
183
+ /**
184
+ * The command line of each `docker exec` running in *names* now, for telling a session what a
185
+ * stop killed (SW-856). Empty when none run or the daemon can't be read: a stop never waits on it.
186
+ */
187
+ export declare function runningExecCommands(names: string[]): Promise<string[]>;
183
188
  export interface ExecResult {
184
189
  exitCode: number;
185
190
  stdout: string;
package/dist/docker.js CHANGED
@@ -531,6 +531,27 @@ export async function copyFileToContainer(container, hostPath, containerPath) {
531
531
  .getContainer(container)
532
532
  .putArchive(pack, { path: path.dirname(containerPath) }));
533
533
  }
534
+ /**
535
+ * The command line of each `docker exec` running in *names* now, for telling a session what a
536
+ * stop killed (SW-856). Empty when none run or the daemon can't be read: a stop never waits on it.
537
+ */
538
+ export async function runningExecCommands(names) {
539
+ const out = [];
540
+ for (const name of names) {
541
+ try {
542
+ const { ExecIDs } = await docker().getContainer(name).inspect();
543
+ for (const id of ExecIDs ?? []) {
544
+ const e = await docker().getExec(id).inspect();
545
+ if (e.Running)
546
+ out.push([e.ProcessConfig.entrypoint, ...e.ProcessConfig.arguments].join(" "));
547
+ }
548
+ }
549
+ catch {
550
+ // Gone already, or no daemon: nothing to name.
551
+ }
552
+ }
553
+ return out;
554
+ }
534
555
  /**
535
556
  * Run a command inside a running container and capture what it wrote.
536
557
  *
@@ -26,7 +26,7 @@ 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). */
29
+ /** `docker inspect` lines (`feature\tworkdir\tstartedAt\tmemory\tnanoCpus\texecs`) the scheduler reads as running features (SW-685). */
30
30
  containers?: string[];
31
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. */
32
32
  execCwd?: string;
@@ -71,6 +71,8 @@ export interface FixtureContainer {
71
71
  /** Bytes reserved. */
72
72
  memory: number;
73
73
  nanoCpus: number;
74
+ /** `docker exec`s running in it (SW-856): the scheduler treats any as work in flight. */
75
+ execs?: number;
74
76
  }
75
77
  export interface FixturePresets {
76
78
  /** An approved PR for a one-file change in packages/lib, with `demo-lib` running, for `pr gate`. */
@@ -88,9 +88,9 @@ export function fixturePreset({ gate, bare, change, ...opts }) {
88
88
  ? c.workspace
89
89
  : Number.isNaN(Date.parse(c.startedAt))
90
90
  ? c.startedAt
91
- : [c.memory, c.nanoCpus].find((n) => !Number.isSafeInteger(n) || n < 0);
91
+ : [c.memory, c.nanoCpus, c.execs ?? 0].find((n) => !Number.isSafeInteger(n) || n < 0);
92
92
  if (bad !== undefined)
93
- throw new Error(`--running: bad value ${JSON.stringify(bad)}: feature and workspace are lowercase names, startedAt a date, memory and nanoCpus whole numbers`);
93
+ throw new Error(`--running: bad value ${JSON.stringify(bad)}: feature and workspace are lowercase names, startedAt a date, memory, nanoCpus and execs whole numbers`);
94
94
  }
95
95
  const badEntry = opts.queue?.find((q) => !QUEUE_ENTRY.test(q));
96
96
  if (badEntry !== undefined)
@@ -267,7 +267,7 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
267
267
  const containers = (opts.containers ?? []).map((c) => {
268
268
  const ws = path.join(root, "ws", c.workspace);
269
269
  fs.mkdirSync(ws, { recursive: true });
270
- return [c.feature, ws, c.startedAt, c.memory, c.nanoCpus].join("\t");
270
+ return [c.feature, ws, c.startedAt, c.memory, c.nanoCpus, c.execs ?? 0].join("\t");
271
271
  });
272
272
  fs.mkdirSync(home, { recursive: true });
273
273
  const fakes = writeFakes(path.join(root, "bin"), {
package/dist/generate.js CHANGED
@@ -14,7 +14,7 @@ import * as path from "node:path";
14
14
  import { isDeepStrictEqual } from "node:util";
15
15
  import nunjucks from "nunjucks";
16
16
  import * as YAML from "yaml";
17
- import { candidateServiceNames } from "./archetypes.js";
17
+ import { candidateServiceNames, withComposeMounts } from "./archetypes.js";
18
18
  import { applyCaches } from "./build_caches.js";
19
19
  import { projectName } from "./compose.js";
20
20
  import { COMPOSE_FILE, migrateWorkspaceCompose } from "./compose_file.js";
@@ -1428,7 +1428,9 @@ export async function generateWorkspaceCompose(feature, servicesStr, workspaceRo
1428
1428
  const [portStart, portEnd] = getPortRange(slot);
1429
1429
  success(`Compose ready (slot ${slot}, ports ${portStart}-${portEnd})`);
1430
1430
  const reposConfig = loadReposConfig();
1431
- const contextMd = buildSandboxContextMd(feature, slot, portStart, portEnd, localServices, servicesConfig, testPath, null, candidateServiceNames(repoNames, reposConfig, localServices), readAutoStop(workspaceRoot), tempHomeWrapper(workspaceRoot), reposConfig);
1431
+ const contextMd = buildSandboxContextMd(feature, slot, portStart, portEnd, localServices,
1432
+ // SW-872: the mounts compose gives (fragment, dev_links), as the allow rules count them.
1433
+ withComposeMounts(servicesConfig, workspaceRoot, workspaceServices(servicesConfig, workspaceRoot)), testPath, null, candidateServiceNames(repoNames, reposConfig, localServices), readAutoStop(workspaceRoot), tempHomeWrapper(workspaceRoot), reposConfig);
1432
1434
  const sandboxDir = dotDir(workspaceRoot);
1433
1435
  fs.mkdirSync(sandboxDir, { recursive: true });
1434
1436
  fs.writeFileSync(path.join(sandboxDir, "werkmeister-context.md"), contextMd);
@@ -17,9 +17,9 @@ export const cli = (rule) => CLI_NAMES.map((name) => `Bash(${name} ${rule})`);
17
17
  // shortcut: prefix rules — `npm install foo -g`, an agent-edited test script or a repo's own git
18
18
  // hook still runs (SW-522); a runner without host mounts is the upgrade if runs need a hard boundary.
19
19
  export const AUTONOMOUS_DENY = [
20
- // SW-560: `Bash(git:*)` is allowed, and an alias, pager, fsmonitor or hook set through config, or a
20
+ // SW-560: the git subcommands a run needs are allowed, and an alias, pager, fsmonitor or hook set through config, or a
21
21
  // git-* binary on --exec-path, runs any command. No `* -c *`/`* config *` forms: deny beats allow, and they
22
- // refuse `git commit -m "fix config"`; `git -C <dir> -c …` is SW-626's.
22
+ // refuse `git commit -m "fix config"`. SW-626: the allow rules name a subcommand right after `git` or `-C <repo>`, so `-C <dir> -c …` asks.
23
23
  "Bash(git -c:*)",
24
24
  "Bash(git config:*)",
25
25
  ...["--config", "--exec-path", "--template"].flatMap((flag) => [
@@ -27,6 +27,10 @@ export const AUTONOMOUS_DENY = [
27
27
  `Bash(git * ${flag}*)`,
28
28
  ]),
29
29
  "Bash(git --git-dir*)",
30
+ // SW-626: options of allowed subcommands that reach past the repo: diff/log/show write any path with
31
+ // `--output`, and `diff --no-index` reads any. git takes no abbreviation of either; `grep -O` and fetch's
32
+ // `--upload-pack` do (`-lO`, `--open`, `--upl`), so grep isn't allowed and fetch is exact instead.
33
+ ...["--output", "--no-index"].map((flag) => `Bash(git * ${flag}*)`),
30
34
  // SW-521: a registered test chain's bare `node --test` segment (buildAllowRules) runs any file
31
35
  // with the real HOME; npx and pnpm exec segments name their tool, so they stay allowed.
32
36
  "Bash(node:*)",
@@ -78,6 +82,17 @@ export const AUTONOMOUS_DENY = [
78
82
  ].map((cmd) => `Bash(${pm} ${cmd}:*)`)),
79
83
  ...["add -g", "add --global", "remove -g", "remove --global"].map((cmd) => `Bash(pnpm ${cmd}:*)`),
80
84
  "Bash(gh pr merge:*)",
85
+ // SW-625: the next wake's rules follow these (FEATURE, dev_links, the record), so the agent can't widen them.
86
+ // Only these files: the gate's logs are redirected into .werkmeister/ (pr-rules.md rule 3).
87
+ ...[
88
+ ".env*",
89
+ ".werkmeister/workspace.yml",
90
+ ".werkmeister/claude-permissions.json",
91
+ ".werkmeister/token",
92
+ ].map((f) => `Edit(/${f})`),
93
+ // SW-626: a repo's config (fsmonitor, aliases, hooksPath) and hooks run on the allowed git commands.
94
+ "Edit(/.git/**)",
95
+ "Edit(/**/.git/**)",
81
96
  ...[
82
97
  "kube-switch",
83
98
  "up",
@@ -89,12 +89,15 @@ export declare function removeWorkspaceHooks(workspaceRoot: string): string[];
89
89
  /** The SessionEnd auto-stop entry while *on*; otherwise remove it, keeping hand-added ones. */
90
90
  export declare function writeAutoStopHook(workspaceRoot: string, on: boolean): string;
91
91
  export declare const READ_ONLY_GIT_RULES: string[];
92
+ /** The git rules, bare or with `-C <dir>` for one of *dirs* (the workspace's own repos). */
93
+ export declare const gitRules: (dirs?: string[]) => string[];
92
94
  export declare const AUTONOMOUS_RULES: string[];
93
95
  /**
94
96
  * AUTONOMOUS_RULES plus the `werkmeister` and `docker` commands that name this feature or its containers.
95
97
  * *repoServices*: the workspace's repos' other services (on-demand ones), which only `feature up --service` starts.
98
+ * *hostMounted*: services whose start runs their `command:` with host mounts; none of their starts is pre-approved.
96
99
  */
97
- export declare function autonomousRules(feature: string, services: string[], repoServices?: string[]): string[];
100
+ export declare function autonomousRules(feature: string, services: string[], repoServices?: string[], hostMounted?: string[]): string[];
98
101
  /**
99
102
  * Allow rules for each local service's registered test command, then read-only
100
103
  * git. Each command gets the container form the agent is told to run, plus the
@@ -43,17 +43,18 @@
43
43
  */
44
44
  import * as fs from "node:fs";
45
45
  import * as path from "node:path";
46
- import { parse as parseYaml } from "yaml";
47
- import { candidateServiceNames, containerTestCommand, hostAccess, isPlainChain, serviceTestCommand, wrappedTestCommand, } from "../../archetypes.js";
48
- import { dotDir, getReposRoot, loadArchetypes, loadReposConfig, loadServices, sandboxRoot, } from "../../config.js";
46
+ import { isDeepStrictEqual } from "node:util";
47
+ import { candidateServiceNames, containerTestCommand, hostAccess, isPlainChain, serviceTestCommand, withComposeMounts, wrappedTestCommand, } from "../../archetypes.js";
48
+ import { dotDir, getReposRoot, loadArchetypes, loadReposConfig, loadServices, } from "../../config.js";
49
49
  import { frozenInstalls } from "../../fragment_detect/workspace.js";
50
50
  import { load as loadGlobalConfig } from "../../global_config.js";
51
51
  import { workspaceServices } from "../../registry_sync.js";
52
52
  import { info } from "../../reporter.js";
53
53
  import { repoDirs } from "../../standards.js";
54
54
  import { tempHomeWrapper } from "../../temp_home.js";
55
- // biome-ignore lint/suspicious/noImportCycles: jira→harness→…→issue_tracker (SW-188); SW-411
56
- import { readAutonomous, readAutoStop, readGrants, readModel } from "../../workspace_prompts.js";
55
+ // biome-ignore-start lint/suspicious/noImportCycles: jira→harness→…→issue_tracker (SW-188); SW-411
56
+ import { readAutonomous, readAutoStop, readGrants, readModel, readWorkspaceField, } from "../../workspace_prompts.js";
57
+ // biome-ignore-end lint/suspicious/noImportCycles: SW-188
57
58
  import { AUTONOMOUS_DENY, cli, pluginTool } from "../autonomous_deny.js";
58
59
  import { CODEX_HOOKS_FILE, CODEX_LAUNCH, writeCodexConfig } from "../codex/index.js";
59
60
  // biome-ignore lint/suspicious/noImportCycles: jira→harness→…→issue_tracker (SW-188); SW-411
@@ -345,26 +346,79 @@ export const READ_ONLY_GIT_RULES = [
345
346
  // `werkmeister:*`, `docker:*`, `npm:*`, `gh:*` or whole MCP server, which reached
346
347
  // the shared proxy, other features and the linked CLI. The PR guard is a
347
348
  // PreToolUse deny, so `gh pr create` stays blocked.
349
+ // SW-854: what decides the container a start runs; a branch that changes one loses `unattended_start`.
350
+ const START_FIELDS = [
351
+ "command",
352
+ "image",
353
+ "build",
354
+ "entrypoint",
355
+ "archetype",
356
+ "extra_volumes",
357
+ "environment",
358
+ ];
359
+ const WHOLE_FEATURE_STARTS = ["up", "restart", "feature up", "feature restart"];
360
+ // SW-626: the git a run needs, not `git:*`, which ran commands through `rebase -x`, `bisect run`,
361
+ // `submodule foreach`, `difftool -x` and `-C <other workspace>`. Push is `werkmeister pr open`'s.
362
+ const GIT_PREFIXES = [
363
+ "status",
364
+ "diff",
365
+ "log",
366
+ "show",
367
+ "add",
368
+ "commit",
369
+ "restore",
370
+ "branch",
371
+ "rev-parse",
372
+ "ls-files",
373
+ "merge-base",
374
+ "rev-list",
375
+ "switch",
376
+ "checkout",
377
+ "symbolic-ref",
378
+ "worktree list",
379
+ ];
380
+ // Exact: rebase's pr-rules.md rule 3 catch-up (any other rebase takes `-x <cmd>`); fetch takes any URL, path
381
+ // or `--upl=<cmd>`; `remote -v add|set-url` dispatches the subcommand after `-v`. No grep: the Grep tool covers it.
382
+ const GIT_EXACT = [
383
+ "rebase origin/main",
384
+ "rebase --continue",
385
+ "rebase --abort",
386
+ "fetch",
387
+ "fetch origin",
388
+ "fetch origin main",
389
+ "remote -v",
390
+ "remote get-url origin",
391
+ ];
392
+ /** The git rules, bare or with `-C <dir>` for one of *dirs* (the workspace's own repos). */
393
+ export const gitRules = (dirs = []) => ["", ...dirs.map((d) => `-C ${d} `)].flatMap((c) => [
394
+ ...GIT_PREFIXES.map((s) => `Bash(git ${c}${s}:*)`),
395
+ ...GIT_EXACT.map((s) => `Bash(git ${c}${s})`),
396
+ ]);
348
397
  export const AUTONOMOUS_RULES = [
349
- "Bash(git:*)",
398
+ ...gitRules(),
350
399
  // `werkmeister pr open` does the push and the PR; gh is read-only here.
351
400
  ...["pr view", "pr checks", "pr diff", "pr list", "issue view", "run view", "run list"].map((cmd) => `Bash(gh ${cmd}:*)`),
352
401
  // No-arg forms act on the cwd's FEATURE; `autonomousRules` adds the named ones.
353
402
  // Each pre-SW-186 spelling keeps its rule until the CLI drops it, beside its new one.
354
- ...["up", "restart", "feature up", "feature restart"].flatMap(cli),
403
+ ...WHOLE_FEATURE_STARTS.flatMap(cli),
355
404
  ...[
356
405
  "status",
357
406
  "feature status",
358
407
  "infra status",
359
- "pr",
408
+ // SW-625: no `pr watch`: `--install` writes a host launchd job, and a wake resumes every workspace.
409
+ "pr gate",
410
+ "pr open",
411
+ "pr status",
412
+ "pr new",
413
+ "pr adopt",
360
414
  "issue view",
361
415
  "issue start",
362
- "init-workspace",
363
- "workspace init",
364
416
  "skills",
365
417
  "standards",
366
418
  "notes context",
367
419
  ].flatMap((cmd) => cli(`${cmd}:*`)),
420
+ // SW-625: exact; `--feature <other>` repoints .env at another workspace's containers, `--repos`/`--force`/`--autonomous` reshape it.
421
+ ...["init-workspace", "workspace init"].flatMap((cmd) => ["", " --refresh-issue", " --prune-context", " --labels focus"].flatMap((flag) => cli(`${cmd}${flag}`))),
368
422
  "Bash(docker ps:*)",
369
423
  // Local build and test; no `npm install` or `pnpm add`, so a new dependency waits for a person.
370
424
  // SW-521: no `node`, `npx`, `pnpm exec` or `python3`, which run any file with the real HOME;
@@ -390,24 +444,34 @@ export const AUTONOMOUS_RULES = [
390
444
  /**
391
445
  * AUTONOMOUS_RULES plus the `werkmeister` and `docker` commands that name this feature or its containers.
392
446
  * *repoServices*: the workspace's repos' other services (on-demand ones), which only `feature up --service` starts.
447
+ * *hostMounted*: services whose start runs their `command:` with host mounts; none of their starts is pre-approved.
393
448
  */
394
- export function autonomousRules(feature, services, repoServices = []) {
449
+ export function autonomousRules(feature, services, repoServices = [], hostMounted = []) {
450
+ // SW-854: a start or restart runs the service's `command:`, often a package script the agent can edit.
451
+ const startable = (svc) => !hostMounted.includes(svc);
452
+ const wholeFeature = services.every(startable);
453
+ const base = wholeFeature
454
+ ? AUTONOMOUS_RULES
455
+ : AUTONOMOUS_RULES.filter((r) => !WHOLE_FEATURE_STARTS.flatMap(cli).includes(r));
395
456
  if (!feature)
396
- return AUTONOMOUS_RULES;
457
+ return base;
397
458
  return [
398
- ...AUTONOMOUS_RULES,
459
+ ...base,
399
460
  // Exact: `werkmeister feature restart <f> infra` and `docker restart <f>-a <other>` stay out.
400
- ...["up", "restart", "feature up", "feature restart"].flatMap((cmd) => cli(`${cmd} ${feature}`)),
461
+ ...(wholeFeature ? WHOLE_FEATURE_STARTS.flatMap((cmd) => cli(`${cmd} ${feature}`)) : []),
401
462
  // SW-132: one of its own services, the way the PR gate starts them. SW-843: exact, since a
402
463
  // trailing `<other>` or `-w <path>` would start another workspace's container.
403
- ...[...services, ...repoServices].flatMap((svc) => [`feature up --service ${svc}`, `feature up ${feature} --service ${svc}`].flatMap(cli)),
404
- ...["logs", "test", "services", "feature logs", "feature test", "feature services"].flatMap((cmd) => cli(`${cmd} ${feature}:*`)),
405
- // SW-560: no `docker exec <f>-<svc>:*`: any container reaches the host daemon over
406
- // host.docker.internal, and some mount the docker socket; its exact registered test still runs.
407
- ...services.flatMap((svc) => [
408
- `Bash(docker logs ${feature}-${svc}:*)`,
409
- `Bash(docker restart ${feature}-${svc})`,
410
- ]),
464
+ ...[...services, ...repoServices]
465
+ .filter(startable)
466
+ .flatMap((svc) => [`feature up --service ${svc}`, `feature up ${feature} --service ${svc}`].flatMap(cli)),
467
+ // SW-855: exact too. No `feature test` even exact: it runs the workspace's own `playwright.config.*`,
468
+ // which the agent can write, with the real HOME; its `-w <other> --scaffold` writes another workspace.
469
+ ...["logs", "services", "feature logs", "feature services"].flatMap((cmd) => cli(`${cmd} ${feature}`)),
470
+ ...services.flatMap((svc) => ["logs", "feature logs"].flatMap((cmd) => cli(`${cmd} ${feature} ${svc}`))),
471
+ // SW-560: no `docker exec <f>-<svc>:*`: some containers mount the docker socket, and any one that
472
+ // mounts the workspace reaches the werkmeister daemon with its token (SW-855); its exact registered test still runs.
473
+ ...services.map((svc) => `Bash(docker logs ${feature}-${svc}:*)`),
474
+ ...services.filter(startable).map((svc) => `Bash(docker restart ${feature}-${svc})`),
411
475
  ];
412
476
  }
413
477
  // Permission strings can't carry a marker the way hook commands do, so the rules
@@ -469,37 +533,6 @@ hostServices = [], reposConfig = {}) {
469
533
  .map((r) => `${wrapped}--no-wait ${r.slice(wrapped.length)}`));
470
534
  return [...new Set([...rules, ...READ_ONLY_GIT_RULES])];
471
535
  }
472
- /**
473
- * SW-845: *registry* with each entry's `extra_volumes` widened to every bind compose mounts: the
474
- * branch's own fragment and the workspace's `dev_links` too (both agent-editable), variables
475
- * substituted as generate.ts does. Only the mounts: test commands stay the registry's.
476
- */
477
- function withComposeMounts(registry, workspaceRoot) {
478
- const branch = workspaceServices(registry, workspaceRoot);
479
- // generate.ts's loadDevLinks reads the same field; importing it closes a cycle (generate → claude_md → jira → here).
480
- let devLinks = {};
481
- try {
482
- const yml = parseYaml(fs.readFileSync(path.join(dotDir(workspaceRoot), "workspace.yml"), "utf8"));
483
- devLinks = yml?.["dev_links"] ?? {};
484
- }
485
- catch {
486
- // No workspace.yml (or unreadable): no dev_links, as generate.ts reads it.
487
- }
488
- return Object.fromEntries(Object.entries(registry).map(([id, entry]) => {
489
- const repo = path.join(workspaceRoot, String(branch[id]?.["repo"] ?? entry["repo"] ?? ""));
490
- const specs = [
491
- ...(entry.extra_volumes ?? []),
492
- ...(branch[id]?.extra_volumes ?? []),
493
- ...(devLinks[id] ?? []),
494
- ].map((s) => {
495
- const out = s.split("${WORKSPACE}").join(workspaceRoot).split("${REPO}").join(repo);
496
- return out.includes("${WERKMEISTER_ROOT}")
497
- ? out.split("${WERKMEISTER_ROOT}").join(sandboxRoot())
498
- : out;
499
- });
500
- return [id, { ...entry, extra_volumes: specs }];
501
- }));
502
- }
503
536
  function _readEnv(workspaceRoot) {
504
537
  let text;
505
538
  try {
@@ -547,7 +580,10 @@ export function workspaceAllowRules(workspaceRoot, autonomous = readAutonomous(w
547
580
  }
548
581
  const extra = hostServices.filter((s) => !services.includes(s));
549
582
  const any = services.length > 0 || extra.length > 0;
550
- const rules = buildAllowRules(any ? feature : "", services, any ? withComposeMounts(loadServices(), workspaceRoot) : {}, any ? loadArchetypes() : {}, wrapper, builtClis, extra, reposConfig);
583
+ const loaded = any ? loadServices() : {};
584
+ const branch = any ? workspaceServices(loaded, workspaceRoot) : {};
585
+ const registry = any ? withComposeMounts(loaded, workspaceRoot, branch) : {};
586
+ const rules = buildAllowRules(any ? feature : "", services, registry, any ? loadArchetypes() : {}, wrapper, builtClis, extra, reposConfig);
551
587
  rules.push(...readGrants(workspaceRoot).flatMap(cli));
552
588
  if (!autonomous)
553
589
  return rules;
@@ -556,7 +592,27 @@ export function workspaceAllowRules(workspaceRoot, autonomous = readAutonomous(w
556
592
  // `yarnPath` would otherwise grant itself an install that runs its own file.
557
593
  const reposRoot = getReposRoot();
558
594
  const installs = repoNames.flatMap((name) => frozenInstalls((f) => fs.existsSync(path.join(reposRoot, name, f))).map((c) => `Bash(${c})`));
559
- return [...new Set([...rules, ...autonomousRules(feature, services, extra), ...installs])];
595
+ // `unattended_start` comes from the machine registry (synced from main), never the branch's fragment, and
596
+ // holds only while the branch runs the same container: its fragment's `command:`/`image:` win in compose (SW-337).
597
+ const unchanged = (svc) => START_FIELDS.every((k) => isDeepStrictEqual(loaded[svc]?.[k], branch[svc]?.[k]));
598
+ const hostMounted = Object.keys(registry).filter((svc) => hostAccess(registry[svc], workspaceRoot) &&
599
+ !(registry[svc]?.["unattended_start"] && unchanged(svc)));
600
+ // SW-626: `-C` only into the workspace's own repos, by absolute path or by name from the workspace root.
601
+ const repoGit = gitRules(repoDirs(workspaceRoot).flatMap((d) => [d, path.basename(d)])).filter((r) => r.startsWith("Bash(git -C "));
602
+ // SW-815: a follow-up the run finds, linked to this workspace's issue; `create` is Jira-only.
603
+ const issueId = String(readWorkspaceField(workspaceRoot, "issue_id") ?? "").trim();
604
+ const followUp = /^[A-Z][A-Z0-9_]*-\d+$/.test(issueId)
605
+ ? cli(`issue create --relates ${issueId}:*`)
606
+ : [];
607
+ return [
608
+ ...new Set([
609
+ ...rules,
610
+ ...autonomousRules(feature, services, extra, hostMounted),
611
+ ...repoGit,
612
+ ...installs,
613
+ ...followUp,
614
+ ]),
615
+ ];
560
616
  }
561
617
  function builtCliWrappers(workspaceRoot) {
562
618
  return repoDirs(workspaceRoot)
@@ -110,6 +110,8 @@ export declare function startService(feature: string, workspace: string, service
110
110
  grant?: boolean;
111
111
  waitMs?: number;
112
112
  }): Promise<void>;
113
+ /** One deadline for all of a `pr gate` / `pr open` run's service starts (SW-869): giving up drops the queue place, so wait longer in place rather than ask for a rerun. */
114
+ export declare const GATE_CAPACITY_WAIT_MS: number;
113
115
  /**
114
116
  * Admit starting *service* (and what compose starts with it) against the budget,
115
117
  * waiting in the queue up to 10 min (SW-540): since SW-342 these starts are the
@@ -128,7 +130,7 @@ export declare function admitServiceStart(feature: string, workspace: string, se
128
130
  grant?: boolean;
129
131
  }): Promise<void>;
130
132
  export declare const RANK_WORD: Record<Rank, string>;
131
- /** A running lease for a person to read: `name (3 CPUs, 0.8 GiB, idle, gate running)`. */
133
+ /** A running lease for a person to read: `name (3 CPUs, 0.8 GiB, idle, run in flight)`. */
132
134
  export declare function describeLease(l: Lease): string;
133
135
  /**
134
136
  * Who holds the budget, the head *feature* waits behind, and what happens next: the same lines for a
@@ -200,6 +202,8 @@ export interface RunningFeature {
200
202
  memory: number;
201
203
  /** Σ `HostConfig.NanoCpus`, cores. */
202
204
  cpus: number;
205
+ /** `docker exec`s running in its containers now (`.ExecIDs`: Docker drops an exec when it exits). */
206
+ execs: number;
203
207
  }
204
208
  export interface IdleInput {
205
209
  minutes: number;
@@ -226,7 +230,9 @@ export interface StopIdleOptions {
226
230
  only?: string;
227
231
  now?: number;
228
232
  run?: (cmd: string[]) => RunResult;
229
- stop?: (feature: string) => Promise<unknown>;
233
+ stop?: (feature: string, opts: {
234
+ why: string;
235
+ }) => Promise<unknown>;
230
236
  /** Override for whether anyone waits for capacity; default: the queue file. */
231
237
  queued?: boolean;
232
238
  }
@@ -273,6 +279,11 @@ export declare function admitOrQueue(feature: string, workspace: string, byPerso
273
279
  export declare function grantWaiters(): Promise<string[]>;
274
280
  /** The daemon's side of a client's release (SW-185 D3): start waiters without holding the caller. */
275
281
  export declare function grantInBackground(): number;
282
+ /**
283
+ * What a session hears when werkmeister stopped its containers under these commands (SW-856):
284
+ * they died with them (exit 137), so the session reruns them rather than reading a test failure.
285
+ */
286
+ export declare function killedNudge(why: string, killed: string[]): string;
276
287
  /**
277
288
  * Stop *feature*'s containers, leaving them and its slot in place so
278
289
  * `restart`/`up` can resume it.
@@ -280,8 +291,9 @@ export declare function grantInBackground(): number;
280
291
  * Throws `NOT_RUNNING` when the feature holds no slot, and
281
292
  * `COMPOSE_STOP_FAILED` when a container fails to stop.
282
293
  */
283
- export declare function stop(feature: string, { grant }?: {
294
+ export declare function stop(feature: string, { grant, why }?: {
284
295
  grant?: boolean;
296
+ why?: string;
285
297
  }): Promise<StopResult>;
286
298
  /**
287
299
  * Restart *feature*'s containers in place.
package/dist/lifecycle.js CHANGED
@@ -26,7 +26,7 @@
26
26
  import * as fs from "node:fs";
27
27
  import * as path from "node:path";
28
28
  import { setTimeout as delay } from "node:timers/promises";
29
- import { agentOf, clearPaused, clearWaiting, nudgePath, readPaused, readSessions, readWaiting, STALE_SESSION_MS, writeNudge, writePaused, writeWaiting, } from "./agent_state.js";
29
+ import { agentOf, claimLastStop, clearPaused, clearWaiting, nudgePath, readPaused, readSessions, readWaiting, STALE_SESSION_MS, writeLastStop, writeNudge, writePaused, writeWaiting, } from "./agent_state.js";
30
30
  import { ensureSharedNetwork, isCaddyRunning, reloadCaddy, startCaddy } from "./caddy.js";
31
31
  import { Colors } from "./colors.js";
32
32
  import {
@@ -35,7 +35,7 @@ import {
35
35
  composeDown, composeDownAsync, composeRestart, composeStop, composeUp, composeUpAsync, composeUpServiceAsync, dependencyOrder, getContainerStatus, isOneShotService, liveProject, workspaceForFeature, } from "./compose.js";
36
36
  import { COMPOSE_FILE, migrateWorkspaceCompose } from "./compose_file.js";
37
37
  import { getVolumeBaseNames, loadPorts, loadServices, needsCluster, sharedProxySkipReason, } from "./config.js";
38
- import { docker, listVolumeNames, projectContainers, removeVolume } from "./docker.js";
38
+ import { docker, listVolumeNames, projectContainers, removeVolume, runningExecCommands, } from "./docker.js";
39
39
  import { dotDir } from "./dot_dir.js";
40
40
  import { SandboxError } from "./errors.js";
41
41
  import { ingressHostnames, loadFeatureRouters, warnUnresolvedHosts, } from "./feature_routing.js";
@@ -187,9 +187,17 @@ export async function up(feature, workspace, { signal, byPerson } = {}) {
187
187
  requireSharedNetwork();
188
188
  info("Starting containers...");
189
189
  if (!(await withStartRecord(workspace, "up", () => composeUpAsync(feature, signal)))) {
190
- throw new SandboxError("Failed to start containers. Check docker compose output above.", "COMPOSE_UP_FAILED");
190
+ throw new SandboxError(
191
+ // Not "above": a grant runs this in another process, and its nudge carries this text (SW-861).
192
+ `Failed to start containers: \`werkmeister feature logs ${feature}\` shows why.`, "COMPOSE_UP_FAILED");
191
193
  }
192
194
  containersStarted = true;
195
+ // Up now: two admits return before the one that dequeues (Docker unreadable, a zero-cost start), and
196
+ // a leftover entry keeps the hook saying "queued" over running containers (SW-863), as in restart().
197
+ await dequeue(feature);
198
+ clearWaiting(workspace);
199
+ // Its stop is answered by this start; a later grant must not report it (SW-702). The grant claims it first.
200
+ claimLastStop(workspace);
193
201
  }
194
202
  else {
195
203
  info("No local services to start -- routing entirely to k8s");
@@ -326,11 +334,13 @@ export async function startService(feature, workspace, service, composeName = CO
326
334
  if (!started) {
327
335
  // A timed-out or cancelled up leaves the container Created, never started (SW-86).
328
336
  const state = (await getContainerStatus(feature)).find((c) => c.service === service)?.status;
329
- throw new SandboxError(`Failed to start ${service}${state ? ` (left ${state})` : ""} — see the compose output above`, "COMPOSE_UP_FAILED");
337
+ throw new SandboxError(`Failed to start ${service}${state ? ` (left ${state})` : ""}: \`werkmeister feature logs ${feature} ${service}\` shows why`, "COMPOSE_UP_FAILED");
330
338
  }
331
339
  }
332
340
  const SERVICE_POLL_MS = 15_000;
333
341
  const SERVICE_WAIT_MS = 10 * 60_000;
342
+ /** One deadline for all of a `pr gate` / `pr open` run's service starts (SW-869): giving up drops the queue place, so wait longer in place rather than ask for a rerun. */
343
+ export const GATE_CAPACITY_WAIT_MS = 30 * 60_000;
334
344
  /**
335
345
  * Admit starting *service* (and what compose starts with it) against the budget,
336
346
  * waiting in the queue up to 10 min (SW-540): since SW-342 these starts are the
@@ -360,7 +370,12 @@ export async function admitServiceStart(feature, workspace, service, { sleep = d
360
370
  // Never byPerson: an agent's gate in a person's workspace must not stop another run's containers.
361
371
  let position = await admitOrQueue(feature, workspace, false, starting);
362
372
  // Not in a person's workspace: their running app must not go down because they asked for one more service.
363
- if (position !== null && ownRunning.length && !released.length && readAutonomous(workspace)) {
373
+ // Nor in the grant pass (SW-873): with no wait it would stop its own set and drop it, never parked.
374
+ if (position !== null &&
375
+ grant &&
376
+ ownRunning.length &&
377
+ !released.length &&
378
+ readAutonomous(workspace)) {
364
379
  // Release instead of hold (SW-675): two gates each holding core while waiting for one more
365
380
  // service deadlock a full budget. Stopped, not removed: one request then brings back the set.
366
381
  await stop(feature, { grant: false });
@@ -390,7 +405,7 @@ export async function admitServiceStart(feature, workspace, service, { sleep = d
390
405
  writeWaiting(workspace, { ...waiting, parked: { service } });
391
406
  parked = true;
392
407
  }
393
- throw new SandboxError(`No capacity to start ${services.join(", ")} for '${feature}' after ${waitMs / 60_000} min ` +
408
+ throw new SandboxError(`No capacity to start ${services.join(", ")} for '${feature}' after ${Math.round(waitMs / 60_000)} min ` +
394
409
  `(queue position ${position})${parked ? "; it stays queued" : ""}.` +
395
410
  (released.length
396
411
  ? ` ${[...up].join(", ")} stayed stopped; \`werkmeister feature up\` starts the feature again.`
@@ -400,7 +415,7 @@ export async function admitServiceStart(feature, workspace, service, { sleep = d
400
415
  if (!waited) {
401
416
  warn(
402
417
  // SW-734: say how a wait the person doesn't want ends.
403
- `Waiting for capacity to start ${services.join(", ")}: position ${position}, up to ${waitMs / 60_000} min. ` +
418
+ `Waiting for capacity to start ${services.join(", ")}: position ${position}, up to ${Math.round(waitMs / 60_000)} min. ` +
404
419
  "Stopping the command ends the wait; `pr gate` and `pr open` start no service the PR file's `skip_tests` names.");
405
420
  }
406
421
  await sleep(SERVICE_POLL_MS);
@@ -429,9 +444,9 @@ export const RANK_WORD = {
429
444
  working: "working",
430
445
  person: "a person's workspace",
431
446
  };
432
- /** A running lease for a person to read: `name (3 CPUs, 0.8 GiB, idle, gate running)`. */
447
+ /** A running lease for a person to read: `name (3 CPUs, 0.8 GiB, idle, run in flight)`. */
433
448
  export function describeLease(l) {
434
- return `${l.feature} (${cpus(l.cost.cpus)}, ${gib(l.cost.memory)}, ${RANK_WORD[l.rank]}${l.busy ? ", gate running" : ""})`;
449
+ return `${l.feature} (${cpus(l.cost.cpus)}, ${gib(l.cost.memory)}, ${RANK_WORD[l.rank]}${l.busy ? ", run in flight" : ""})`;
435
450
  }
436
451
  /**
437
452
  * Who holds the budget, the head *feature* waits behind, and what happens next: the same lines for a
@@ -587,6 +602,9 @@ export function otherLiveSessions(workspaceRoot, sessionId) {
587
602
  }
588
603
  /** Why a running feature is not idle, or null when it is. */
589
604
  function busyReason(f, input, cutoff) {
605
+ // A test run or probe in its container is work in flight, whatever the session record says (SW-856).
606
+ if (f.execs)
607
+ return "docker exec running";
590
608
  // An open session that has sat idle past the cutoff does not count (SW-39 D3), nor, while others
591
609
  // queue, one that has waited on a person that long: its status stays busy through the ask (SW-675).
592
610
  const recent = (s) => (s.statusUpdatedAt ?? Infinity) > cutoff;
@@ -641,19 +659,27 @@ export function runningFeatures(runCmd = (cmd) => run(cmd, { capture: true, time
641
659
  if (!list.size)
642
660
  return [];
643
661
  const feature = `or (index .Config.Labels "${LABEL_FEATURE}") (index .Config.Labels "${LEGACY_LABEL_FEATURE}")`;
644
- const fmt = `{{${feature}}}\t{{index .Config.Labels "${LABEL_WORKDIR}"}}\t{{.State.StartedAt}}\t{{.HostConfig.MemoryReservation}}\t{{.HostConfig.NanoCpus}}`;
662
+ const fmt = `{{${feature}}}\t{{index .Config.Labels "${LABEL_WORKDIR}"}}\t{{.State.StartedAt}}\t{{.HostConfig.MemoryReservation}}\t{{.HostConfig.NanoCpus}}\t{{len .ExecIDs}}`;
645
663
  const inspect = runCmd(["docker", "inspect", "--format", fmt, ...list]);
646
664
  if (inspect.status !== 0)
647
665
  return null;
648
666
  const byFeature = new Map();
649
667
  for (const line of inspect.stdout.split("\n").filter(Boolean)) {
650
- const [feature, workspace, started, memory, nanoCpus] = line.split("\t");
668
+ const [feature, workspace, started, memory, nanoCpus, execs] = line.split("\t");
651
669
  if (!feature || !workspace)
652
670
  continue;
653
- const f = byFeature.get(feature) ?? { feature, workspace, startedAt: [], memory: 0, cpus: 0 };
671
+ const f = byFeature.get(feature) ?? {
672
+ feature,
673
+ workspace,
674
+ startedAt: [],
675
+ memory: 0,
676
+ cpus: 0,
677
+ execs: 0,
678
+ };
654
679
  f.startedAt.push(Date.parse(started));
655
680
  f.memory += Number(memory) || 0;
656
681
  f.cpus += (Number(nanoCpus) || 0) / 1e9;
682
+ f.execs += Number(execs) || 0;
657
683
  byFeature.set(feature, f);
658
684
  }
659
685
  return [...byFeature.values()];
@@ -699,7 +725,7 @@ export async function stopIdle(minutes, opts = {}) {
699
725
  }
700
726
  const asking = input.sessions(f.workspace).some((s) => s.asking);
701
727
  try {
702
- await (opts.stop ?? stop)(f.feature);
728
+ await (opts.stop ?? stop)(f.feature, { why: `idle ≥ ${minutes} min` });
703
729
  lines.push(asking
704
730
  ? `${f.feature}: stopped (waiting on a person ≥ ${minutes} min, others queued)`
705
731
  : `${f.feature}: stopped (idle ≥ ${minutes} min)`);
@@ -733,11 +759,19 @@ const GRANTED_NUDGE = "Capacity freed: this workspace's containers are up. Conti
733
759
  * The granted session's nudge. Preemption stops on-demand containers too, and the grant's `up` starts
734
760
  * only the default set, so name each one left stopped and how to start it (SW-840).
735
761
  */
736
- async function grantedNudge(feature) {
762
+ async function grantedNudge(feature, last) {
737
763
  const stopped = (await getContainerStatus(feature)).filter((c) => c.status !== "running" && !isOneShotService(c.service, c.role));
764
+ // Say why it was down at all (SW-702): nothing else tells the session who stopped it.
765
+ const why = last
766
+ ? `Werkmeister had stopped it (${last.why})` +
767
+ (last.killed.length
768
+ ? `, killing ${last.killed.map((c) => `\`${c}\``).join(", ")}: rerun them`
769
+ : "") +
770
+ ". "
771
+ : "";
738
772
  if (!stopped.length)
739
- return GRANTED_NUDGE;
740
- return (`Capacity freed: this workspace started, but these containers are still stopped: ${stopped.map((c) => c.name).join(", ")}. ` +
773
+ return why + GRANTED_NUDGE;
774
+ return (`${why}Capacity freed: this workspace started, but these containers are still stopped: ${stopped.map((c) => c.name).join(", ")}. ` +
741
775
  `Start them with ${stopped.map((c) => `\`werkmeister feature up --service ${c.service}\``).join(", ")}, ` +
742
776
  "then continue where you left off.");
743
777
  }
@@ -768,7 +802,9 @@ export function runningLeases(runCmd) {
768
802
  : state?.agent === "working"
769
803
  ? "working"
770
804
  : "idle";
771
- const busy = readPaused(f.workspace) !== null ||
805
+ // A running `docker exec` (a session's test run, not only the gate's) is never stopped (SW-856).
806
+ const busy = f.execs > 0 ||
807
+ readPaused(f.workspace) !== null ||
772
808
  (mtimeMs(lastExecMarker(f.workspace)) ?? 0) > gateCutoff;
773
809
  const lease = {
774
810
  feature: f.feature,
@@ -817,7 +853,7 @@ async function admitOrQueueHere(feature, workspace, byPerson, starting) {
817
853
  const victimWs = readLeases()[victim]?.workspace || workspaceForFeature(victim);
818
854
  try {
819
855
  // stop, not pause: a paused container keeps its memory.
820
- await stop(victim, { grant: false });
856
+ await stop(victim, { grant: false, why: `preempted to make room for '${feature}'` });
821
857
  }
822
858
  catch (e) {
823
859
  // A failed stop can still have killed it (SW-738): queue it to come back, and wait rather than
@@ -867,46 +903,57 @@ async function grantWaitersNow() {
867
903
  // A lease pruned by a process that can't see the folder must not cost the waiter its start (SW-848).
868
904
  const workspace = readLeases()[feature]?.workspace || workspaceForFeature(feature);
869
905
  if (!workspace) {
906
+ // No folder to nudge in: say it here, at least (SW-873).
907
+ warn(`'${feature}' left the waiting queue: its workspace can't be found`);
870
908
  await dequeue(feature);
871
909
  continue;
872
910
  }
873
- const waiting = readWaiting(workspace);
874
- if (waiting?.parked) {
875
- if (await startParked(feature, workspace, waiting.parked.service)) {
876
- started.push(feature);
877
- if (liveSessions(workspace).length)
878
- writeNudge(workspace, await grantedNudge(feature));
879
- }
880
- continue;
881
- }
882
- // A service start's own poll admits it; `up` would start the wrong set and dequeue it.
883
- if (waiting?.services)
884
- continue;
885
- let r;
911
+ // Before up(), which drops the record on any start, so only this grant's nudge reports it (SW-702).
912
+ const last = claimLastStop(workspace);
886
913
  try {
887
- r = await up(feature, workspace, { byPerson: false });
888
- }
889
- catch (e) {
890
- // One waiter's failed re-up (HOST_LOAD, NO_COMPOSE, ...) must not end the pass for the rest (SW-737).
891
- const message = `Could not start '${feature}' from the waiting queue: ${e.message}`;
892
- warn(message);
893
- // Admission already dropped it (never fits, or a start that failed after admit): only its session can act (SW-682, SW-848).
894
- if (!readQueue().includes(feature)) {
895
- clearWaiting(workspace);
896
- // A never-fits rerun would fail the same way; its message already says what to change.
897
- const neverFits = e instanceof SandboxError && e.code === "NEVER_FITS";
898
- writeNudge(workspace, neverFits
899
- ? message
900
- : `${message}\nIt left the queue: run \`werkmeister feature up\` to queue it again.`);
914
+ const waiting = readWaiting(workspace);
915
+ if (waiting?.parked) {
916
+ if (await startParked(feature, workspace, waiting.parked.service)) {
917
+ started.push(feature);
918
+ if (liveSessions(workspace).length)
919
+ writeNudge(workspace, await grantedNudge(feature, last));
920
+ }
921
+ continue;
901
922
  }
902
- continue;
923
+ // A service start's own poll admits it; `up` would start the wrong set and dequeue it.
924
+ if (waiting?.services)
925
+ continue;
926
+ let r;
927
+ try {
928
+ r = await up(feature, workspace, { byPerson: false });
929
+ }
930
+ catch (e) {
931
+ // One waiter's failed re-up (HOST_LOAD, NO_COMPOSE, ...) must not end the pass for the rest (SW-737).
932
+ const message = `Could not start '${feature}' from the waiting queue: ${e.message}`;
933
+ warn(message);
934
+ // Admission already dropped it (never fits, or a start that failed after admit): only its session can act (SW-682, SW-848).
935
+ if (!readQueue().includes(feature)) {
936
+ clearWaiting(workspace);
937
+ // A never-fits rerun would fail the same way; its message already says what to change.
938
+ const neverFits = e instanceof SandboxError && e.code === "NEVER_FITS";
939
+ writeNudge(workspace, neverFits
940
+ ? message
941
+ : `${message}\nIt left the queue: run \`werkmeister feature up\` to queue it again.`);
942
+ }
943
+ continue;
944
+ }
945
+ // Backfill (SW-675): a later waiter may fit; admission itself holds the room for a starved head.
946
+ if (r.waiting !== undefined)
947
+ continue;
948
+ started.push(feature);
949
+ if (liveSessions(workspace).length)
950
+ writeNudge(workspace, await grantedNudge(feature, last));
951
+ }
952
+ finally {
953
+ // Still waiting: the grant that starts it reports the stop.
954
+ if (last && !started.includes(feature) && readQueue().includes(feature))
955
+ writeLastStop(workspace, last);
903
956
  }
904
- // Backfill (SW-675): a later waiter may fit; admission itself holds the room for a starved head.
905
- if (r.waiting !== undefined)
906
- continue;
907
- started.push(feature);
908
- if (liveSessions(workspace).length)
909
- writeNudge(workspace, await grantedNudge(feature));
910
957
  }
911
958
  return started;
912
959
  }
@@ -923,7 +970,9 @@ async function startParked(feature, workspace, service) {
923
970
  });
924
971
  }
925
972
  catch (e) {
926
- if (!(e instanceof SandboxError && e.code === "NO_CAPACITY")) {
973
+ // No room keeps it parked; any other way out of the queue its session must hear (SW-873).
974
+ if (!(e instanceof SandboxError && e.code === "NO_CAPACITY") ||
975
+ !readQueue().includes(feature)) {
927
976
  const message = `Could not start '${feature}' ${service} from the waiting queue: ${e.message}`;
928
977
  warn(message);
929
978
  // Its session was told it continues once started; it must hear that it won't (SW-701 review).
@@ -955,6 +1004,15 @@ async function grantWaitersQuietly() {
955
1004
  warn(`Could not start the next waiting feature: ${e instanceof Error ? e.message : String(e)}`);
956
1005
  }
957
1006
  }
1007
+ /**
1008
+ * What a session hears when werkmeister stopped its containers under these commands (SW-856):
1009
+ * they died with them (exit 137), so the session reruns them rather than reading a test failure.
1010
+ */
1011
+ export function killedNudge(why, killed) {
1012
+ return (`Werkmeister stopped this workspace's containers (${why}) while these ran in them: ` +
1013
+ `${killed.map((c) => `\`${c}\``).join(", ")}. They were killed (exit 137), not failed: start the ` +
1014
+ "containers with `werkmeister feature up` (`--service <name>` for an on-demand one), then rerun them.");
1015
+ }
958
1016
  /**
959
1017
  * Stop *feature*'s containers, leaving them and its slot in place so
960
1018
  * `restart`/`up` can resume it.
@@ -962,9 +1020,26 @@ async function grantWaitersQuietly() {
962
1020
  * Throws `NOT_RUNNING` when the feature holds no slot, and
963
1021
  * `COMPOSE_STOP_FAILED` when a container fails to stop.
964
1022
  */
965
- export async function stop(feature, { grant = true } = {}) {
1023
+ export async function stop(feature, { grant = true, why } = {}) {
966
1024
  refuseInsideOwnContainer(feature, "stop");
967
1025
  const slot = await requireSlot(feature);
1026
+ // *why* marks a stop nobody in the workspace asked for: name what it kills to the session (SW-856).
1027
+ const workspace = why ? workspaceForFeature(feature) : null;
1028
+ if (why && workspace) {
1029
+ const names = (await getContainerStatus(feature))
1030
+ .filter((c) => c.status === "running")
1031
+ .map((c) => c.name);
1032
+ const killed = await runningExecCommands(names);
1033
+ // Best effort: a folder gone, or unseen from a container (SW-860), must not block the stop.
1034
+ try {
1035
+ writeLastStop(workspace, { at: new Date().toISOString(), why, killed });
1036
+ if (killed.length)
1037
+ writeNudge(workspace, killedNudge(why, killed));
1038
+ }
1039
+ catch (e) {
1040
+ warn(`Could not tell '${feature}' why it stopped: ${e.message}`);
1041
+ }
1042
+ }
968
1043
  info(`Stopping containers for '${feature}' (slot ${slot})...`);
969
1044
  if (!(await composeStop(feature))) {
970
1045
  throw new SandboxError(`Failed to stop containers for '${feature}'`, "COMPOSE_STOP_FAILED");
@@ -19,7 +19,7 @@ import { GATE_COMPOSE_FILE, renderGateCompose } from "./generate.js";
19
19
  import { updateNoteStatus } from "./graduate.js";
20
20
  import { eachIssue, issueStatuses, moveIssue } from "./issue_status.js";
21
21
  import { groupOf, issueIdOf, issueRefsOf, issueTracker, projectOf } from "./issue_tracker.js";
22
- import { grantWaiters, idleStopMinutes, lastExecMarker, liveSessions, readServicesFromWorkspace, runningFeatures, startService, stop as stopFeature, stopIdle, } from "./lifecycle.js";
22
+ import { GATE_CAPACITY_WAIT_MS, grantWaiters, idleStopMinutes, lastExecMarker, liveSessions, readServicesFromWorkspace, runningFeatures, startService, stop as stopFeature, stopIdle, } from "./lifecycle.js";
23
23
  import { originSlug, prWatchScheduleState, } from "./machine_setup.js";
24
24
  import { openDecisions } from "./notes.js";
25
25
  import { prRulesPath } from "./pr_guard.js";
@@ -58,6 +58,8 @@ async function httpStatus(url) {
58
58
  }
59
59
  }
60
60
  export function defaultDeps() {
61
+ // Built once per command, so the wait is a total across its starts, not per start (SW-869).
62
+ const capacityDeadline = Date.now() + GATE_CAPACITY_WAIT_MS;
61
63
  return {
62
64
  run: execRun,
63
65
  httpStatus,
@@ -100,7 +102,10 @@ export function defaultDeps() {
100
102
  const granted = await grantWaiters().catch((e) => [`waiting queue: ${e.message}`]);
101
103
  return { lines: [...lines, ...granted.map((f) => `${f}: started from the waiting queue`)] };
102
104
  },
103
- startService: (ws, feature, service, composeName) => startService(feature, ws, service, composeName, { skipCurrentSetups: true }),
105
+ startService: (ws, feature, service, composeName) => startService(feature, ws, service, composeName, {
106
+ skipCurrentSetups: true,
107
+ waitMs: Math.max(0, capacityDeadline - Date.now()),
108
+ }),
104
109
  portHolders: (ws, feature, services) => portHolders(feature, ws, services),
105
110
  gateCompose: (ws, feature, services) => renderGateCompose(feature, Number(readEnv(ws, "WERKMEISTER_SLOT")), readServicesFromWorkspace(ws), services, ws),
106
111
  };
@@ -3,7 +3,7 @@
3
3
  * ▶ Start work, `werkmeister workspace prompt --args` (the next-wave skill) and an autonomous BRIEF.md.
4
4
  */
5
5
  export declare const INTERACTIVE_LINE = "This is an interactive run: check with me at each stage's human check.";
6
- export declare const GATE_LINE = "Run `werkmeister feature up` only when the gate needs it, and `werkmeister feature stop` this workspace's containers right after `werkmeister pr open` succeeds.";
6
+ export declare const GATE_LINE = "Run `werkmeister feature up` only when the gate, the exercise or a measurement needs the container, and `werkmeister feature stop` this workspace's containers right after `werkmeister pr open` succeeds.";
7
7
  /** What an autonomous run may not do, as markdown bullets. */
8
8
  export declare const RUN_LIMITS: string;
9
9
  /** Drop machine-writing run steps from a starting prompt and say they happen after merge instead. */
package/dist/run_start.js CHANGED
@@ -3,10 +3,10 @@
3
3
  * ▶ Start work, `werkmeister workspace prompt --args` (the next-wave skill) and an autonomous BRIEF.md.
4
4
  */
5
5
  export const INTERACTIVE_LINE = "This is an interactive run: check with me at each stage's human check.";
6
- export const GATE_LINE = "Run `werkmeister feature up` only when the gate needs it, and `werkmeister feature stop` this workspace's containers right after `werkmeister pr open` succeeds.";
6
+ export const GATE_LINE = "Run `werkmeister feature up` only when the gate, the exercise or a measurement needs the container, and `werkmeister feature stop` this workspace's containers right after `werkmeister pr open` succeeds.";
7
7
  /** What an autonomous run may not do, as markdown bullets. */
8
8
  export const RUN_LIMITS = [
9
- "Tests never call the real Jira or GitHub APIs; the tracker is mocked. The only real issue updates are the status moves `werkmeister issue start` and `werkmeister pr open` make for this ticket and its `group:` members; run both.",
9
+ "Tests never call the real Jira or GitHub APIs; the tracker is mocked. The only real issue updates are the status moves `werkmeister issue start` and `werkmeister pr open` make for this ticket and its `group:` members; run both. A follow-up the run finds is filed with `werkmeister issue create --relates <this ticket>` (SW-815); no other tracker writes.",
10
10
  "Never read or write the real `~/.werkmeister`; tests use a temporary HOME. To see the machine config's shape run `werkmeister machine config`: it masks secrets such as `jira.api_token`.",
11
11
  "No `npm link`, `npm`/`pnpm install -g`, `npm publish`, `werkmeister infra proxy-reload`, and no other workspaces.",
12
12
  'Where the ticket says "to confirm", use it as written unless the code shows a reason to change it, and record each choice in the stage output for PR review.',
@@ -19,7 +19,7 @@ export interface Lease {
19
19
  feature: string;
20
20
  cost: Cost;
21
21
  rank: Rank;
22
- /** Mid-gate or paused: stopping it would break a run in flight. */
22
+ /** A `docker exec` running in it, mid-gate or paused: stopping it would break a run in flight. */
23
23
  busy: boolean;
24
24
  }
25
25
  export interface Request {
package/dist/slots.d.ts CHANGED
@@ -53,6 +53,8 @@ export declare function getAllSlotsFromDocker(): Promise<Record<string, SlotEntr
53
53
  /** Reads Docker labels; injectable so tests need no daemon. */
54
54
  export interface SlotDeps {
55
55
  labels: () => Promise<Record<string, SlotEntry> | null>;
56
+ /** Docker's marker file, for tests; default `/.dockerenv`. */
57
+ dockerenv?: string;
56
58
  }
57
59
  /**
58
60
  * Allocate *feature*'s slot and lease it before the lock is released.
package/dist/slots.js CHANGED
@@ -25,6 +25,7 @@ import { workspaceForFeature } from "./compose.js";
25
25
  import { baselineUpstream, loadPorts, loadServices, sandboxHome, slotPortRange } from "./config.js";
26
26
  import { docker } from "./docker.js";
27
27
  import { SandboxError } from "./errors.js";
28
+ import { insideContainer } from "./global_config.js";
28
29
  import { sandboxLock } from "./locks.js";
29
30
  import { error, info, success, warn } from "./reporter.js";
30
31
  import { pickSlot } from "./scheduler.js";
@@ -175,11 +176,15 @@ const defaultSlotDeps = { labels: getAllSlotsFromDocker };
175
176
  * label and a lease disagree on a slot's owner the label wins: those
176
177
  * containers are what bind its ports.
177
178
  */
178
- function heldSlots(dockerSlots) {
179
+ function heldSlots(dockerSlots, dockerenv) {
179
180
  const leases = readLeases();
180
- for (const [f, l] of Object.entries(leases)) {
181
- if (l.workspace && !fs.existsSync(l.workspace))
182
- delete leases[f];
181
+ // A container mounts the real ~/.werkmeister but only its own workspace: every other folder
182
+ // reads gone from there, so only the host prunes (SW-860).
183
+ if (!insideContainer(dockerenv)) {
184
+ for (const [f, l] of Object.entries(leases)) {
185
+ if (l.workspace && !fs.existsSync(l.workspace))
186
+ delete leases[f];
187
+ }
183
188
  }
184
189
  for (const [f, e] of Object.entries(dockerSlots ?? {})) {
185
190
  leases[f] ??= { slot: e.slot, workspace: workspaceForFeature(f) ?? "" };
@@ -210,7 +215,7 @@ export function allocateSlot(feature, preferredSlot, workspace, deps = defaultSl
210
215
  function allocateSlotHere(feature, preferredSlot, workspace, deps) {
211
216
  const count = slotCount();
212
217
  return sandboxLock("allocate", async () => {
213
- const { leases, held } = heldSlots(await deps.labels());
218
+ const { leases, held } = heldSlots(await deps.labels(), deps.dockerenv);
214
219
  const slot = pickSlot(feature, held, count, preferredSlot ?? null);
215
220
  if (slot === null) {
216
221
  error(`All ${count} feature slots are occupied!`);
@@ -350,7 +350,7 @@ const bugfix = [
350
350
  `When the proposed fix branches on a predicate, list each condition that makes it true and give each one a test. ${PREAPPROVED_INPUT}`,
351
351
  NAME_HISTORY_STEP,
352
352
  // SW-564: sw-521's root cause was a permission rule; step 4 listed the callers of one builder and missed two other sources of a matching rule.
353
- "When the root cause is a permission rule (allow, deny, ask), step 4 lists every source of a matching rule: each builder, each rule derived from a registered command (its `&&` segments included), rules recorded in existing workspaces and how a refresh or migrate removes them, and the list a headless wake passes (`--allowedTools` / `--disallowedTools`). The failing test probes each source.",
353
+ "When the root cause is a permission rule (allow, deny, ask), step 4 lists every source of a matching rule: each builder, each rule derived from a registered command (its `&&` segments included), rules recorded in existing workspaces and how a refresh or migrate removes them, and the list a headless wake passes (`--allowedTools` / `--disallowedTools`). The failing test probes each source. Also `git grep -F` the exact old rule text across the whole repo, tests and `apps/` included, and list each hit as a site the fix must update (SW-820).",
354
354
  `Write \`output/repro.md\`: steps 1–7, and a proposed fix in one paragraph under a \`status: proposed\` line.${noCommitPlan(v.group)} Check the proposed fix against the standards rows above and name any rule that already prescribes it. ${CRITERION_REACH}`,
355
355
  ],
356
356
  outputs: ["`output/repro.md`", "One failing test, uncommitted"],
@@ -153,6 +153,13 @@ export declare function initWorkspace(args: Record<string, unknown>, workspaceRo
153
153
  /** False when the workspace names a feature but no slot was free, so compose.yml was not generated. */
154
154
  containers: boolean;
155
155
  }>;
156
+ export declare const CONTRACTS_CHANGED_NUDGE = "`werkmeister workspace init` just regenerated this workspace's contracts (CLAUDE.md, CONTEXT.md, `_shared/`, the stage CONTEXT.md files; committed on the record branch as `chore: regenerate workspace contracts`). Re-read CLAUDE.md and the current stage's CONTEXT.md before going on: their rules now govern. An instruction the person gave you in chat still wins; if the two conflict, ask.";
157
+ /**
158
+ * SW-760: a rerun rewrites the contracts under any live session. Commit them alone on the record
159
+ * branch, so they don't ride along with the agent's next commit, and leave the session a nudge.
160
+ * Only the generated rule files: stage outputs and notes are the agent's.
161
+ */
162
+ export declare function recordRegeneratedContracts(workspaceRoot: string, owned: string[]): boolean;
156
163
  /**
157
164
  * Create a workspace from already-resolved inputs and initialise it. Returns
158
165
  * the workspace directory.
@@ -27,6 +27,7 @@ import * as fs from "node:fs";
27
27
  import * as os from "node:os";
28
28
  import * as path from "node:path";
29
29
  import * as YAML from "yaml";
30
+ import { writeNudge } from "./agent_state.js";
30
31
  import { presentArchetypes } from "./archetypes.js";
31
32
  import { Colors } from "./colors.js";
32
33
  import { ensureSharedVolumes, retireLegacyProject, workspaceComposeArgv } from "./compose.js";
@@ -43,6 +44,7 @@ import { harnessLayoutFor, parseHarnessId } from "./harness/layout.js";
43
44
  import { HARNESS_IDS } from "./harness/types.js";
44
45
  import { fetchIssueSnapshot, IssueFetchError, readIssueRef, writeIssueCopy, } from "./issue_snapshot.js";
45
46
  import { issueTracker, projectForIssue } from "./issue_tracker.js";
47
+ import { liveSessions } from "./lifecycle.js";
46
48
  import { defaultWorkspaceRepo, originMismatch, WORKSPACE_ROOT_REPO, workspaceRepoUrl, } from "./machine_setup.js";
47
49
  import { resolveWorkspaceMcpServers, validateArchetypeMcpServers, writeWorkspaceMcp, } from "./mcp_gen.js";
48
50
  import { modelTiers } from "./model_tiers.js";
@@ -659,6 +661,7 @@ export async function initWorkspace(args, workspaceRootOverride, opts = {}) {
659
661
  success(`Wrote ${writeFixtureWrapper(workspaceRoot)}`);
660
662
  // 5b–6c. The context layer — CLAUDE.md and everything the agent reads
661
663
  // beside it — belongs to the workspace's strategy (docs/context-strategies.md).
664
+ const rerun = fs.existsSync(path.join(workspaceRoot, "CLAUDE.md"));
662
665
  strategy.init({
663
666
  workspaceRoot,
664
667
  workspaceName,
@@ -679,6 +682,8 @@ export async function initWorkspace(args, workspaceRootOverride, opts = {}) {
679
682
  continue;
680
683
  reportLeftovers(workspaceRoot, strategyById(id), strategy, Boolean(args["prune_context"]));
681
684
  }
685
+ if (rerun)
686
+ recordRegeneratedContracts(workspaceRoot, strategy.ownedPaths());
682
687
  // 6d needs the present archetypes whichever strategy ran.
683
688
  const [servicesConfig, reposConfig] = loadSandboxServiceConfigs();
684
689
  const archetypes = loadArchetypes();
@@ -766,6 +771,29 @@ export async function initWorkspace(args, workspaceRootOverride, opts = {}) {
766
771
  "Open a new Claude Code session from the workspace root to pick up the context.\n\n");
767
772
  return { containers };
768
773
  }
774
+ export const CONTRACTS_CHANGED_NUDGE = "`werkmeister workspace init` just regenerated this workspace's contracts (CLAUDE.md, CONTEXT.md, `_shared/`, the stage CONTEXT.md files; committed on the record branch as `chore: regenerate workspace contracts`). Re-read CLAUDE.md and the current stage's CONTEXT.md before going on: their rules now govern. An instruction the person gave you in chat still wins; if the two conflict, ask.";
775
+ /**
776
+ * SW-760: a rerun rewrites the contracts under any live session. Commit them alone on the record
777
+ * branch, so they don't ride along with the agent's next commit, and leave the session a nudge.
778
+ * Only the generated rule files: stage outputs and notes are the agent's.
779
+ */
780
+ export function recordRegeneratedContracts(workspaceRoot, owned) {
781
+ const stageContracts = (dir) => fs.existsSync(path.join(workspaceRoot, dir))
782
+ ? fs
783
+ .readdirSync(path.join(workspaceRoot, dir))
784
+ .map((s) => path.join(dir, s, "CONTEXT.md"))
785
+ .filter((f) => fs.existsSync(path.join(workspaceRoot, f)))
786
+ : [];
787
+ const paths = owned
788
+ .flatMap((p) => (p === "stages" ? stageContracts(p) : [p]))
789
+ .filter((p) => !p.includes("*") && !p.endsWith("/notes"))
790
+ .filter((p) => fs.existsSync(path.join(workspaceRoot, p)));
791
+ if (!paths.length || !commitRecord(workspaceRoot, "chore: regenerate workspace contracts", paths))
792
+ return false;
793
+ if (liveSessions(workspaceRoot).length)
794
+ writeNudge(workspaceRoot, CONTRACTS_CHANGED_NUDGE);
795
+ return true;
796
+ }
769
797
  // ---------------------------------------------------------------------------
770
798
  // init-workspace hand-off
771
799
  // ---------------------------------------------------------------------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "werkmeister-core",
3
- "version": "0.95.1",
3
+ "version": "0.95.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.",